Initial contact with Swift

Source: Internet
Author: User

Swift does not have a main function without a placeholder for the string array dictionary in OC, etc. all need to be used @ but Swift is not used @

Common types of data
Let B1:int = 10
Let b2:double = 1.22
Let b3:string = "SDF"
var b4:character = "a"

To find the length of a string
Different from OC???
OC directly. Count gets the string length and Swift is the first to get the number of characters in the string
var str = "string"
Print (Str.characters.count)

Stitching strings
Different from OC
OC is used stringWithFormat: stitching or appendingwithstring:
Swift is divided into the following two types
1> string and string concatenation
var str1 = "Good"
var str2 = "Morning"
Print ("\ (str1) \ (str2)")//or as follows
STR1 + = str2
Print (STR1)
2> string and character stitching
Let S:character = "a"
Str.append (s)
Print (str)

is equal = =
Basic data types in OC with = = string with isequaltostring: curly braces can be omitted if there is no other option after if is judged
In Swift, either the string or the base data type is used = = The parentheses after the IF in the If judgment can be omitted but the curly braces must be preserved
if (str = = "good") {
Print (str)
}

prefix/suffix
Str.hasprefix ("St")
Str.hassuffix ("ing")

Uppercase and lowercase conversions
var str3 = "GOOGLE"
Print ("Convert to lowercase \ (str3.lowercasestring)")
Print ("converted to uppercase \ (str3.uppercasestring)")

String Support traversal
For item in Str3.characters {
Print (item)
}

Convert Swift string to OC object
Let STR4 = Str3 as NSString
Print (STR3)

Array
Create an array be sure to specify the data type of the element

var array:[string] = ["1", "2", "3"]
adding elements
Array.append ("4")
Delete
Array.removeall ()//Remove all elements including array opening space array = Nil
Array.removeall (keepcapacity:true)//Remove all elements but preserve the space opened by the array array.count = 0
Array.removeatindex (2)//Remove the element below the corresponding subscript
Array.removefirst ()//Remove first element
Array.removelast ()//Remove last element
Traversal of an array
For item in array {
Print (item)
}
Meta-group Traversal
For (index, value) in Array.enumerate () {
Print ("index=\ (Index), value=\ (value)")
}
Print (Array.count)//array number
Merging arrays
var arrar1:[string] = ["Mi", "33"]
Array + = Arrar1

Dictionary
Create a dictionary also to indicate the type of dictionary with [] no {}
var dic:[string:anyobject] = ["1": "You", "2": "Wo"]
var dic1:[string:anyobject] = ["2": "F", "3": "SSS", "5": "SDF"]
Adding and updating key-value pairs
dic["3"] = "mm"//If there is a key in the dictionary that is the update if it is not added
Delete a key value pair
Dic.removeall ()//Remove all elements including array open space dic = Nil
Dic.removeall (keepcapacity:true)//Remove all elements but preserve the space opened by the array dic.count = 0
Dic.removeatindex (Dic.indexforkey ("1")!)//Note here! Remove the element below the corresponding subscript
Dic.removevalueforkey ("1")//Remove the element below the corresponding subscript
General convenience
For item in Dic.enumerate () {
Print (item)
}
Meta-group Traversal
for (Index,value) in Dic.enumerate () {
Print ("index=\ (Index), valu=\ (value)")
}
Print (Dic.keys)//All keys
Print (dic.values)//All value
Merging two dictionaries
For item in Dic.keys {
Dic1[item] = Dic[item]
}

Optional type
Optional type: If a value is likely to be empty it may also have a value that value is an optional type optional type to use? Said
Optional types cannot be directly involved in the operation to first force the unpacking (wrapped) with!
Warning must confirm that this optional value is not nil before unpacking an optional type type
Unexpectedly found nil while unwrapping an Optional value describes the mandatory unpacking of nil
In the model, all the basic data types are not considered optional, otherwise the key solution will not be found when the dictionary is passed to the model. A default value of 0 for an optional type
var c1:int? = 1
Let C2 = 2
Let C3 = C1! + C2
Print (C3)

// ?? Air counting identifiers
Function: If the optional type value is not empty?? will automatically help us unpack and then participate in the operation if the optional type value is empty is the use?? The default values provided later are calculated
var value:int?
Let value1 = (value?? 5) + 10
Print ("value1 = \ (value1)")


Enumeration
Enum Month:string {
The first way
Case January = "January"
Case February = "February"
Case Match = "March"
The second way
Case January, February, Match
}
var month = Month.february
month =. February
Print ("month=\ (Month.rawvalue)")//To get the default value corresponding to the enumeration value (January February March)
Print ("month=\ (Month.hashvalue)")//Gets the index of the enumeration value corresponding to

Control flow

If statement
There is no concept of 0 or true in Swift
Let A = 10
If a > 3 {
Print (a)
}

If let
If let first determine if the optional type is empty, create a local constant to receive the value of the optional type at the time of execution code block otherwise do not execute code block equivalent if a = = ten {} else {}
var aa:int?
If let B = AA {
Print (b)
}
If let where
Make a judgment on the defined constants
If let bb = AA where bb > 9 {
Print (BB)
}

Guard Guard
is used to determine if an optional type has a value that is not a value and executes an else similar to the IF let
Func demo () {//no parameter no return function
var a10:int? = 11
Guard Let D = A10 else {
Print ("nil")
Return
}
Print (d)
}
Demo ()//Call

Switch
The parentheses after the switch can omit the case without writing the break will not cause the penetration effect. The default must be added and only added after the last case only one statement {} can be omitted
Switch does not want to be like OC, only the choice of the shape of the selection string float, etc.
Shaping representation
Let A1 = 10
Switch A1 {
Case 2:
Print ("a = 2")
Case 10:
Print ("a = 10")
Default
Print ("nil")
}

String representation
Let string = "Beautiful"
Switch string {
Case "SDB":
Print ("nil")
Case "Beautiful":
Print (String)
Default
Print ("Happyness")
}

For loop
Notation 1
var i = 0
For i = 0; I < 10; i++ {
Print (i)
}
Notation 2
For I in 0...10 {//write format must be written like this
Print (i)
}
Notation 3
For I in 0..<10 {//write format must be written like this
Print (i)
}
While
var j = 10
While I > 1 {
i--
}
There is no do while in Swift

Initial contact with Swift

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.