1.1Converting value
Swift 不会自动类型转换,要想类型转换必须用Int(),Double,String() etc.
var quantity = 42 var unitPrice = 34.55 println("The amount is \(Double(quantity)*unitPrice)") __________________________________
1.2Basic if statements
Swift在条件判断中不需要小括号,只判断true还是false
1.3switch 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; } __________________________________
1.4 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) }
1.5Simple 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
1.6Arrays 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.") }
1.7Dictionaries 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)")
}
1.8tuples 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")