IOS8 开发之Swift - 自学之路(第二天)

Converting value

Swift 不会自动类型转换,要想类型转换必须用Int(),Double,String() etc.

var quantity = 42
var unitPrice = 34.55
println("The amount is \(Double(quantity)*unitPrice)") 
__________________________________



Basic if statements

Swift在条件判断中不需要小括号,只判断true还是false

switch syntax

switch没有case穿透现象,在每个case中没有break关键字。在default中可以有break。

1)Must be exhaustive

2)Can provide ranges of values

3)No implicit fallthrough - code required in all cases

let windSpeed = 5
switch windSpeed {
    case 0...3:
         println("It's very cala.")
    case 4...6:
         println("A little windy.")
    case 7...9:
         println("Blowing a gale!")
    case 10...12:
         println("Batten down the hatches!")
    default:
         break;
}
__________________________________
 

loops in Swift

...closed range operator

0...100    36...99

..< half-open range operator

var total = 0
for index in 1...100 {
    total = total + index
}
total

//using for-in loops with Stings
var name = "Bob"

for eachChar in name {
    println(eachChar)
}

Simple Swift function

:means "is a type of"

传入的参数是常量

func myFunction(name : String = "Jason Wang") {
    println("Hello,\(name)")
}
myFunction("Jane") //ERROR - this will no longer work

myFunction() //OK - use default value
myFunction(name:"Jane") //OK - provide named argument
_______________________________

fuc add(a : Int = 10, b : Int = 50) {
    println("The result is \(a*b)")
}

add(99) // ?compile error
add(a:99) //OK
add(b:200) //OK
add(a:99,b:200) //OK


Arrays in Swift

1)Arrays are zero-based

2)Arrays are typed

3)Mutable when created with var,  inMutable when created with let

var flavors : [String]

//adding to the end of an array
flavors.append("Neaplolitan")
flavors += ["Wintergreen"]

//insert at specific position
flavors.insert("Coconut",atIndex: 3)

//removing items
flavors.removeLast()
flavors.removeAtIndex(3) 

//.count for number of items
println("The array has \(daysInMonth.count) items")

if daysInMonth.isEmpty {
    println("There's nothing in the array.")
}

Dictionaries in Swift

1)AKA Associative Array, Map, Hashtable

var states = ["AZ":"Arizona"]
//Declare dictionary of Int keys and String values
var products : [Int:String]

//Accessing dictionary values
println(states["AZ"])
//Updating or inserting 
states["FL"] = "Florida"//will change OR insert 
states.updateValue("Nevada", forKey:"NV")
//this return any existing value before updating it 
<pre name="code" class="objc">states.updateValue("Nevada", forKey:"AZ")
//to delete key/value pair
states["FL"] = nil
states.removeValueForKey("AZ")

println("There are \(states.count) states left.")

for(abbrev, fullname) in states {
println("\(abbrev) is shor for \(fullname)")
}

tuples in Swift

1)A collectio of elements

var str = "Hello"
let num = 1000

var myTuple = (str, num)
var myOtherTuple = (str, num, 12345, "Some text")

var status = ["AZ":"Arizona","CA":"California"] 

______________________________________________

//returning a tuple
func getCurrentSongAndDuration() -> (name:String, length:Int) {
    return("NoonLight in Vernont",210)
}

//call funcation
let result = getCurrentSongAndDuration()
//decomposing - option 1
println("The song is \(result.name) and it's \(result.length) seconds long")
_____________________________________________________________
<pre name="code" class="objc">func getCurrentSongAndDuration() -> (String, Int) {
    return("NoonLight in Vernont",210)
}

//option2let(name, length) = getCurrentSongAndDuration()
println("The song is \(name) and it's \(length) seconds long") 
 
 IOS8 开发之Swift - 自学之路(第二天)_第1张图片 
 

视频笔记

http://edu.51cto.com/index.php?do=lession&id=26464

Swift概述

Swift是用于设计iOS及Mac OS X应用的一门新语言

Swift特点

  1. Swift保留了C与Objective-C的优点,并摒弃其为了兼容C语言所受的限制。
  2. Swift吸纳了安全编程模式,并新增了一些现代化功能,使其更易使用、更为灵活、更具乐趣。
  3. Swift的简洁风格与广受欢迎的成熟框架Cocoa/Cocoa Touch相结合,姜维重新构思软件的开发过程带来新机遇。
  4. Swift内存管理采用ARC风格。

Swift代码的文件扩展名

*.swift

Objective-c    -----> *.h/*.m /*.mm

第一个Swift程序

使用Xcode project编写

使用playgroud编写

边修改,边运行。

 



你可能感兴趣的:(apple,ios,swift)