Constants, variables
Declaring constants: Let
Declaring variables: var
eg. Let connum = 1; var num = 2;
Basic data types
Int, Float, Double, Bool, tuples, optional
which
Tuples Ganso, can contain a collection of different types of values;
Optional an optional value that indicates that a value can be null (nil), and if a variable is not declared as optional, then it must be given an exact value;
Type conversions
You need to specify a conversion type: Sometype (val)
Operator
have similar rules to other languages
Special rules
- Can be used for the remainder of float, 8 2.5//equals 0.5;
- Empty value operator a?? b, the optional type a null judgment, if a contains a value to be unpacked, otherwise returns a default value B, the expression a must be a optional type, the type of the default value B must be consistent with the type of a stored value
- Interval operator
闭区间运算符(a...b); 半开区间(a..<b
String
Swift's string type is a value type. If you create a new string, the value is copied when it is passed in a constant, variable assignment operation, or in a function/method. In any case, a new copy of the existing string value is created and the new copy is passed or assigned.
Array
var shoppingList: [String] = ["Eggs""Milk"]shoppingList.append("Flour")shoppingList += ["Chocolate Spread","Cheese","Butter"]shoppingList[0"Six eggs"shoppingList.insert("Maple Syrup"0)let mapleSyrup = shoppingList.removeAtIndex(0)forin shoppingList { println(item)}
- Dictionary
varAirports: [String:String] = ["Tyo":"Tokyo","DUB":"Dublin"] for(Airportcode, Airportname)inchAirports {println ("\ (airportcode): \ (airportname)")} forAirportcodeinchAirports.keys {println ("Airport code: \ (airportcode)")}//Airport Code:tyo//Airport CODE:LHR forAirportnameinchairports.values {println ("Airport name: \ (airportname)")}//Airport Name:tokyo//Airport Name:london Heathrow LetAirportcodes =Array(Airports.keys)//Airportcodes is ["Tyo", "LHR"] LetAirportnames =Array(airports.values)//Airportnames is ["Tokyo", "London Heathrow"]
Both arrays and dictionaries store mutable values in a single collection. If we create an array or a dictionary and assign it to a variable, the set will be mutable. This means that we can change the size of this collection by adding more or removing existing data items after creation. Conversely, if we assign an array or dictionary to a constant, it is immutable and its size cannot be changed.
- For loop
Letnames = ["Anna.","Alex","Brian","Jack"] forNameinchNames {println ("Hello, \ (name)!")} LetNumberoflegs = ["Spider":8,"Ant":6,"Cat":4] for(Animalname, Legcount)inchNumberoflegs {println ("\ (animalname) s has \ (legcount) legs")} for varindex =0; Index <3; ++index {println ("index is \ (index)")}
- While, do While
whilecondition { statements}do { whilecondition
- If
var temperatureInFahrenheit = 90if temperatureInFahrenheit <= 32 { println("It‘s very cold. Consider wearing a scarf."elseif temperatureInFahrenheit >= 86 { println("It‘s really warm. Don‘t forget to wear sunscreen."else { println("It‘s not that cold. Wear a t-shirt.")}
- Switch
Let Somecharacter:character ="E"SwitchSomecharacter { Case "a","E","I","O","U":println("\ (somecharacter) is a vowel") Case "B","C","D","F","G","H","J","K","L","M","n","P","Q","R","S","T","V","W","x","Y","Z":println("\ (somecharacter) is a consonant")default:println("\ (Somecharacter) is not a vowel or a consonant")}
Here the default statement is required, otherwise the compilation does not pass, and no break is required because only one case is matched.
You can also add conditions to the case to determine where
let yetanotherpoint = (1 ,-1 ) switch Yetanotherpoint {case let (x, y) where x = = Y:println (" (\ (x), \ (y)) is on the line x = = y ") case let (x, y) where x = =-y:println ( "(\ (x), \ (y)) is on the line x = = y" ) case let (x, y): println (" (\ (x), \ (y)) is just Some arbitrary point ")}//Output" (1,-1) is on the line x = y "
- continue, break, Fallthrough
continue, and break are the same as other language usages;
Fallthrough is used in the switch statement to implement the condition through, Equivalent to the absence of a break in Java or other language case blocks:
let integertodescribe = 5 var Description = "the number \ (Integertodescribe) is" switch integertodescribe {case 2 , 3 , 5 , 7 , one , , Span class= "Hljs-number" > , : Description + = " A prime number, and also " fallthrough default : Description + = "an integer." } println (description) //output "the number 5 is a prime number, and also an integer."
Getting started with Swift (i)