Import Foundation
Single-line Comment
Multiline comments (support nesting, OC is not supported)
Constant let, which cannot be changed after initialization.
The specific type of the constant can be automatically identified, what type is followed by the equal sign, what type it is, and the end does not require a semicolon.
Let TeacherName = "First Name"
Print (TeacherName)
Variable var, which can be re-assigned after initialization
Types can be inferred automatically
var studentname = "First Name"
Studentname = "nickname"
Print (Studentname)
Support Chinese
var programming = "Language"
Print (programming)
Support for emoji special characters
Var?? = "Programmer"
Print (??)
/***************** symbol/Placeholder ******************/
string addition
var string1 = "program"
var string2 = 123
var string3 = string1 + String (string2)//cast type
Print (STRING3)
Placeholder
Let age = 18
Print ("My Age is \")
Specify a type (specify a certain type for a constant or variable, and the assignment must assign a value of the corresponding type after the type is specified)
Let Strcount:uint = 123//Strcount must be unsigned integral type
var string:string = "Hello Swift"
Type aliases (similar to typedef in OC)
Typealias Class02type = Int
Let Int1:int = 123
Let Int2:class02type = 123
Print ("\ (int1)--\ (int2)")
BOOL type. Non-0 in OC is true, there is no such rule in swift, only true and false
var boolValue1 = True
var boolValue2 = False
Tuples (inside elements can be multiple types)
var cakeinfo: (String, Int) = ("Cake", 188)
Print (cakeinfo.0)
Optional type (contains two meanings, if the variable has a value, then the variable is the value, if the variable has no value, then the variable is nil)
// ? Optional type
var optionvalue:string? = "Blue Gull"
// ! Forced parsing must be resolved when it is worthwhile
Print (optionvalue!)
/***************** string and character *****************/
Create a string (with an initial value)
Let string1 = "class"
Create an empty string
Let string2 = ""
Let String3 = String ()
Determines whether a string is empty
If String3.isempty {
Print ("Empty")
}
The string in Swift is a value type, and the string in OC is a reference type
var stringValue1 = "Class"
var stringValue2 = stringValue1
Modify StringValue1
StringValue1 + = "NB"
Print (stringValue1)
Print (stringValue2)
The length of the string
Let stringlength = "Hello Swift"
Print (StringLength.characters.count)
For temp in Stringlength.characters {
Print (temp)
}
To manipulate a string
Stitching string (+ sign stitching or placeholder stitching)
var addstr = "Hello"
var addStr1 = "\ (ADDSTR) World"
Print (ADDSTR1)
Inserting a string
var insertstr = "abc"
Insertstr.insert ("$", AtIndex:insertStr.startIndex)
Insertstr.insert ("@", AtIndex:insertStr.endIndex)
If Advancedby () is positive, is forward, is negative or backward
If you use startindex can not use negative numbers, if the use of endindex can not use a positive number, otherwise it will cross
Insertstr.insert ("#", AtIndex:insertStr.endIndex.advancedBy (-1))
Print (INSERTSTR)
Inserting a string in a string
var insertStr2 = "Hello"
Insertstr2.insertcontentsof ("Swift". Characters, At:insertStr2.endIndex.advancedBy (-5))
Print (INSERTSTR2)
Deletion of strings
var deletestr = "123456"
var deleterange = Deletestr.startindex...deletestr.startindex.advancedby (2)
Deletestr.removerange (Deleterange)
Print (DELETESTR)
Comparison of strings = =
var compareStr1 = "123"
var compareStr2 = "123"
if compareStr1 = = compareStr2 {
Print ("Same")
}else{
Print ("different")
}
/******************* collection type ********************/
Array (var stands for mutable Array, let represents immutable group)
Create an array with an initial value (the type of the value must be uniform)
var arr: [String] = ["iphone", "ipad", "MacBook"]
Create an empty array
var emptyarr = [int] ()//array can only exist in Int type
Determine if the array is empty
If Emptyarr.isempty {
Print ("Empty")
}
Increase of array
Arr.append ("IWatch")
Print (arr)
Insertion of arrays
Arr.insert ("IPod", atindex:1)
Print (arr)
arr + = ["IBook"]//Plus data type to be consistent before and after
Print (arr)
Deletion of arrays
Arr.removeatindex (0)
Arr.removeall ()//delete all elements, but arrays still exist
Print (arr)
Modification of arrays
Arr[0] = "APPLE"
ARR[1...3] = ["APPLE1", "APPLE2", "APPLE3"]
Print (arr)
Traversal of an array
For temp in arr {
Print (temp)
}
for (Tempindex,tempvalue) in Arr.enumerate () {
Print ("Subscript: \ (Tempindex)-value: \ (tempvalue)")
}
Dictionary
Create a dictionary. Dictionary with initial values
var dict = ["Beijing": "China", "Tokyo": "Japan", "Paris": "France"]
Print (DICT)
A dictionary without an initial value
var emptydict = [string:string] ()
Print (emptydict)
Determine if the dictionary is empty
If Emptydict.isempty {
Print ("Empty")
}
Dictionary increment (enter a new key-value pair)
dict["Seoul"] = "Korea"
Print (DICT)
Delete the dictionary (delete the key value)
Dict.removevalueforkey ("Seoul")
Print (DICT)
Dictionary changes (the original dictionary if there is this key value pair, is modified, if not is added)
dict["Beijing" = "Asia"
Print (DICT)
The traversal of a dictionary
For (key, value) in Dict {
Print ("\ (value) capital \ (key)")
}
/******************* Control Flow ********************/
For In loop (1..<6 = less than 6) (1...6) means less than or equal to 6
For value in 1...6 {
Print (value)
}
For loop
for var i = 0; I < 10; i++ {
Print ("i = \ (i)")
}
While loop
var whilevalue = 0
While Whilevalue < 10 {
Print (Whilevalue)
whilevalue++
}
Repeat-while, equivalent to the do-while in OC
var value = 0
Repeat {
Loop body
value++
Print (value)
}while Value < 10
Branch statements
var applevalue = 100
If Applevalue > 10 {
Print ("too expensive")
}else{
Print ("Too cheap")
}
Switch-case
Note: Swift does not require a break and does not run through
If you need to run through, add Fallthrough.
If the case statement cannot contain all cases, then you must add Dafault
The case statement must have at least one line of code, and if not, a break should be added
var switchvalue = 1
Switch Switchvalue {
Case 1:
Print ("Switchvalue = 1")
Fallthrough
Case 2:
Print ("Switchvalue = 2")
Case 3:
Print ("Switchvalue = 3")
Default
Print ("Empty")
}
Switch-case Matching tuples
var point = (10, 10)
Switch Point {
Case (10, _)://_ indicates that this value can be ignored
Print ("Case (10,11)")
Case (_, 0):
Print ("Case (10, 0)")
Default
Print ("other")
}
Switch-case interval Matching
var money = 10
Switch Money {
Case 0...10:
Print ("Poor cock silk")
Case 100...1000:
Print ("Cock wire")
Default
Print ("Local tyrants")
}
Label statement (jumps across the loop according to the label)
Mywhile:while true {
for var i = 0; I < 10; i++ {
if i = = 5 {
Break Mywhile
}
Print ("i = \ (i)")
}
}
/******************* function (method) ********************/
/*
Func function name (parameter list), return value {
function body
}
*/
No parameter no return value. If there is no return value->void can be omitted
Func func1 ()->void {
Print ("Hello Swift")
}
Call
Func1 ()
There are no return values for parameters
Func Func2 (cityname:string) {
Print ("Beijing area belongs to \ (CityName)")
}
Call
Func2 ("North China")
No parameter has return value
Func func3 ()->string{
Return "China"
}
Call
var result = Func3 ()
Print (Result)
There are parameters with return values
Func Func4 (Number1 number1:int, number2:int)->int {
Return number1 + number2
}
Call
var resultvalue = Func4 (number1:10, number2:20)
Print (Resultvalue)
There are parameters with return values (return tuples)
Func Func5 (Number1 number1:int, Number2:int) (Int,int) {
Return (Number1 + number2, Number1 * number2)
}
Call
var resultValue1 = Func5 (Number1:5, Number2:6)
Print (resultValue1)
The formal parameters of a function are constants and cannot be modified. If you want to modify, you need to specify the parameter as a variable plus var
Func changecity (var cityname:string)->void {
CityName = "Sanya"
Print ("Weekend go \ (cityname)")
}
Call
Changecity ("Hainan")
The parameters are changed and the arguments are unchanged. Because the addresses are different.
If you want to change the parameters and arguments at the same time, you need to add the InOut keyword and pass in the address, that is, add & Fetch address character
Func ChangCity2 (inout cityname1:string, inout cityname2:string) {
Let tempcity = cityName1
CityName1 = cityName2
CityName2 = tempcity
Print ("city1=\ (cityName1)->city2=\ (cityName2)")
}
Call
var city1 = "Beijing"
var city2 = "Maldives"
ChangCity2 (&city1, cityName2: &city2)
Print ("Argument: city1=\ (city1)--city2=\ (city2)")
External parameter name and internal parameter name
Func Cityfunc (cityName1 citynmae1:string, cityname2:string, cityname3:string) {
Print ("\ (CITYNMAE1)->\ (cityName2)->\ (cityName3)")
}
Call
Cityfunc (cityName1: "Beijing", CityName2: "Tokyo", CityName3: "Nanjing")
Nested intrinsic functions of functions can only be called internally, and external functions can only be called externally
Func Outfunc (Boolvalue:bool, var number:int) {
Self-added
Func Addfunc () {
Print ("Plus \ (++number)")
}
Self-reduction
Func Jianfunc () {
Print ("Auto minus \ (--number)")
}
Calling intrinsic functions
Boolvalue? Addfunc (): Jianfunc ()
}
Calling external functions
Outfunc (True, number:10)
/******************* Optional Binding ********************/
Use an optional binding to determine whether the intnumber has a value. If there is a value, assign the value to a temporary variable
var intnumber:int? = 10
if var intNumber1 = intnumber {
Print (IntNumber1)
}else{
Print ("No value")
}
var tempstring = "China"
Print (Int (tempstring))
If Let intvalue = Int (tempstring) {
Print (Intvalue)
}else{
Print ("Not a number")
}
/******************* Enumeration ********************/
Define an enumeration with the first letter of the enumeration name capitalized
The hash is not changed by our changes.
Enum city:int{
Case BEIJING = 100
Case NewYork
Case Paris
}
Print (City.Paris.hashValue)
Original value
Print (City.Paris.rawValue)
Enum city1:string{
Case beijing = "Beijing"
Case nanjing = "Nanjing"
Case ShenZhen = "Shenzhen"
}
Print (City1.ShenZhen.hashValue)
Original value
Print (City1.ShenZhen.rawValue)
The tempcity type is City1 value is Beijing created by rawvalue a variable of type City2
Let tempcity = City1 (rawValue: "Beijing")
Print (tempcity!)
Print (tempcity!. RawValue)
Swift Basic Knowledge Point