A. What is Swift? Answer: Baidu.
Two. Basic knowledge of Swift.
1. Output function: Print
Print ("Hello, world! ")
2. Simple data types
Variable declaration: Var
Constant declaration: Let
the
If there is no variable or constant type (1, 2 lines below), the type of the defined variable or constant is automatically determined at the first assignment.
If the type is declared (line 3rd below), you must assign a value to the specified type.
70 70.0
Variable conversions:
1 " "2 943let widthlabel = label + String (width)
string concatenation
String Insert variable method: \ ()
Put the variable in parentheses and it's OK.
1 3 2 5 3 " I have \ (apples) apples. " 4 " I have \ (apples + oranges) pieces of fruit. "
Arrays and dictionaries:
Both the array and the Dictionary objects are placed in [], separated by a "," and the last object can be appended with commas.
Create a dictionary and an array of objects:
1var shoppinglist = ["Catfish","Water","Tulips","Blue paint"]2shoppinglist[1] ="bottle of water"3 4var occupations = [5 "Malcolm":"Captain",6 "Kaylee":"Mechanic",7 ]8occupations["Jayne"] ="Public Relations"
To create an empty array and dictionary:
1 Let Emptyarray = [String] ()2let emptydictionary = [String:float] ()
Or
1 Shoppinglist = []2 occupations = [:]
3. Control Flow
Conditional control: IF and switch
Cyclic control: For-in,for,while and Repeat-while
The parentheses of the statement used for conditional judgment are optional.
For-in:
1Let individualscores = [ the, +,103, the, A]2var Teamscore =03 forScoreinchIndividualscores {4 ifScore > - {5Teamscore + =36}Else {7Teamscore + =18 }9 }TenPrint (Teamscore)
If
1var optionalstring:string? ="Hello"2Print (optionalstring = =Nil)3 4var optionalname:string? ="John Appleseed"5var greeting ="hello!"6 ifLet name =Optionalname {7Greeting ="Hello, \ (name)"8}
Type after adding "? "Represents an optional type (optional value).
If and let use to determine whether the value is nil, if it is nil, then the module will be skipped.
You can manipulate an optional type by using the IF let.
?,?? :
?? The following is the default value
1 Let nickname:string? = Nil2 "John Appleseed"3 "Hi \ ( Nickname?? FullName)"
Use?? You can also manipulate optional types. If the optional type value is nil then use?? The default value later.
Switch
Switch supports multiple types of data types and comparison operations. Not only numeric types and equality judgments.
1Let vegetable ="Red Pepper"2 SwitchVegetable {3 Case "Celery":4Print"Add Some raisins and make ants on a log.")5 Case "Cucumber","Watercress":6Print"That's would make a good tea sandwich.")7 CaseLet XwhereX.hassuffix ("Pepper"):8Print"is it a spicy \ (x)?")9 default:TenPrint"everything tastes good in soup.") One}
When the program finishes executing a switch's case, it jumps out of switch without adding a break.
For-in:
For-in Traversal Dictionary:
1Let interestingnumbers = [2 "Prime": [2,3,5,7, One, -],3 "Fibonacci": [1,1,2,3,5,8],4 "Square": [1,4,9, -, -],5 ]6var largest =07 for(Kind, numbers)inchInterestingnumbers {8 forNumberinchNumbers {9 ifNumber >Largest {Tenlargest = Number One } A } - } -Print (largest)
While and Repeat-while:
1var n =22 whileN < - {3N *=24 }5 print (n)6 7var m =28 Repeat {9M *=2Ten} whileM < - OnePrint (m)
.. < and ...
Cycle start and end boundary judgment, using: < contains the minimum value and not the maximum value. Use... Both the minimum and maximum values are included.
1 0 2 for inch 0.. <4 {3 Total + = I4 }5 Print (total)
Learning resources from Swift official documentation: HTTPS://DEVELOPER.APPLE.COM/LIBRARY/CONTENT/DOCUMENTATION/SWIFT/CONCEPTUAL/SWIFT_PROGRAMMING_ Language/guidedtour.html#//apple_ref/doc/uid/tp40014097-ch2-id1
ios-swift-Basic Article 1