Swift variables and functions, swift Variable Functions
Func getNums ()-> (Int, Int) {// the swift function can return multiple variables return (2, 3)} let (a, B) = getNums () // let is a constant, which cannot be changed once assigned values. var is the println (a) variable. // The output 2var f = getNums // function is an object and can be used as a variable. Copy to another variable println (f () // output (2, 3)
Swift declaration variable var name = "Hello" // name will be automatically recognized as String type
Or specify the variable type: var name: String = "Hello"
In swift, + is used for string connection, but cannot be in the + int type. To add the int type, use the following method:
Var I = 200
Var str = "Hello"
Str = "\ (str), world, \ (I)" // use \ (variable name). The str value is Hello, world, 200.
Different data types can be stored in arrays.
Var arr = ["hello", 100, 2.3]
You can also specify that only Arrays can be stored:
Var arr1 = [] // define an array
Var arr2 = String [] () // The arr2 array can only store strings
Dictionary:
Var dic = ["name": "zhou", "age": "16"]
Dic ["sex"] = "female" // assign a dynamic value to the dictionary
Println (dic) // output [sex: female, name: zhou, age: 16]
Println (dic ["name"]) // outputs zhou
Class Math {class func max (a: Int, B: Int)-> Int {NSLog ("run Math. max ") // print the time, and the string in if (a> B) {return a;} else {return B ;}} var maxNum = Math. max (2, B: 5) println ("Hello, \ (maxNum )")