Func getnums (),(int,int) { //swift function can return multiple variables of Return (2, 3)}let (A, b) = getnums () //let is a constant, once the assignment is immutable, VAR is the variable println (a )= getnums //The function is an object that 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 variable type: var name:string = "Hello"
Swift uses + to make a string connection, but not + int. If you want to add an int type, you can use the following method:
var i = 200
var str = "Hello"
str = "\ (str), world, \ (i)"//use \ (variable name), str value is hello,world,200
Different data types can be stored in the array
var arr = ["Hello", 100, 2.3]
You can also specify that only the array is stored:
var arr1 = []//define an array
var arr2 = string[] ()//ARR2 array can only store strings
Dictionary:
var dic = ["name": "Zhou", "Age": "16"]
dic["sex"] = "female"//assign a value to the dictionary dynamically
println (DIC)//Output [Sex:female, Name:zhou, Age:16]
println (dic["name"])//Output Zhou
classmath{classFunc Max (A:int, B:int)int{NSLog ("Run Math.max")//print time, and inside the stringif(a>b) { returnA; }Else{ returnb; }}}var maxnum= Math.max (2B:5) println ("Hello, \ (maxnum)")
Swift Variables and functions