1.1Converting value
Swift does not automatically type conversions, and you must use INT (), double,string () etc for type conversions.
var quantity = 42var UnitPrice = 34.55println ("The amount is \ (Double (quantity) *unitprice)") ___________________________ _______
1.2Basic IF statements
Swift does not need parentheses in conditional judgment, only true or false
1.3switch syntax
Switch has no case penetration, and there is no break keyword in each case. There can be a break in default.
1) must be exhaustive
2) Can provide ranges of values
3) No implicit fallthrough-code required in all cases
Let windspeed = 5switch 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 = 0for Index in 1...100 {Total = total + index}total//using for-in loops with stingsvar name = ' Bob ' for EA Chchar in name { println (Eachchar)}
1.5Simple Swift function
: means "is a type of"
The parameter passed in is a constant
Func myFunction (name:string = "Jason Wang") { println ("hello,\ (name)")}myfunction ("Jane")//error-this would no Lon Ger workmyfunction ()//ok-use default valuemyfunction (name: "Jane")//ok-provide named argument_______________________ ________fuc Add (A:int = ten, B:int =) { println ("The result is \ (a*b)")}add (a)//? Compile Erroradd (a:99)//oka DD (b:200)//okadd (a:99,b:200)//ok
1.6Arrays in Swift
1) Arrays is zero-based
2) Arrays is typed
3) Mutable when created with Var, inmutable when created and let
var flavors: [String]//adding to the end of an arrayflavors.append ("Neaplolitan") flavors + = ["Wintergreen"]//insert at SP Ecific Positionflavors.insert ("Coconut", Atindex:3)//removing itemsflavors.removelast () Flavors.removeatindex (3)// . Count for number of ITEMSPRINTLN ("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 Valuesvar products: [int:string]//accessing dict Ionary valuesprintln (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= "O BJC ">states.updatevalue (" Nevada ", Forkey:" AZ ")
To delete key/value pair
states["FL"] = Nil
States.removevalueforkey ("AZ")
println ("There is \ (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 = 1000var mytuple = (str, num) var myothertuple = (str, num, 12345, "Some text") var status = ["AZ" : "Arizona", "CA": "California"] ______________________________________________//returning a Tuplefunc Getcurrentsongandduration () (name:string, length:int) { return ("Noonlight in Vernont",}//call) Funcationlet result = Getcurrentsongandduration ()//decomposing-option 1println ("The song is \ (result.name) and it ' s \ (re sult.length) Seconds Long ") _____________________________________________________________<pre name=" code "class= "OBJC" >func getcurrentsongandduration (), (String, Int) { return ("Noonlight in Vernont", "the")}//option2let ( Name, length) = Getcurrentsongandduration () println ("The song is \ (name) and it ' s \ (length) seconds long")
IOS8 development of Swift-self-study road (next day)