Loop OC-style
for
Swift-style
for
- Periodic summary
SwiftUse in keywords to indicate the range of loops
0..<10Represents from 0 to 9
0...10Represents from 0 to 10
- Note that spaces do not appear between
Special wording
- Periodic summary
- If you don't care about the index of the loop itself, you can use the
_ ignore
- This technique has been seen in previous branch walkthroughs
String
In the vast majority of cases in Swift, it is recommended to use the String type
Use
StringThe cause
Stringis a structural body with higher performance
StringCurrently has the most nsstring function
StringSupports direct traversal
NSStringis an OC object with slightly poor performance
SwiftProvides String NSString seamless transitions between and
Traversing strings
string concatenation
- Periodic summary
"" \(变量名) quickly stitch strings in a way that is used directly in
- My little friend and I are never going to think about
stringWithFormat it:D
formatting strings
- Periodic summary
- In real-world development, if you need to specify a string format, you can use
String(format:...) the way
- Note: The following parameters need to be placed in an array
Combination of String & Range
Here's the super-laborious code.
Suggested wording
Array Simple Experience
// 像 OC 一样打印println(arr as NSArray)
Types of objects saved in an array
- Periodic summary
- arrays are defined using [], which is the same as OC
- If all content types are consistent during initialization, the content of that type is saved in the optional array
- If all content types are inconsistent during initialization, the selected array holds the
NSObject
Common Array Operations
- Periodic summary
- If you specify the type of the Save object when you define the array, you cannot add other types of content to the array
- can use
[String]()
letThe defined array is不可变的
varThe defined array is可变的
Dictionary
function Simple Walkthrough
- Periodic summary
- function definition Format:
func 函数名(参数: 参数类型...) -> 返回值 { // 代码实现 }
- If there is no return value,
-> 返回值 you can omit the
->It's a very interesting symbol.
- By default, when a function is called, the first parameter name is omitted
Special handling of parameter names requires parameter names
Omit parameter name
Circular references for closures
Lazy Loading
- Lazy loading is essentially a definition and execution of a closure
Getter & Setter Custom Person class
Getter & Setter
SwiftGetter & Setter in the above form is seldom used
Didset
- In OC, we usually want to do some extra work after assigning a value to a variable.
- The most classic application is to complete the cell's fill with the model's setup method when customizing the cell.
Computed properties
getterproperties that implement only methods are called computed properties, equivalent to properties in OC ReadOnly
- Computed properties themselves do not occupy memory space
- You cannot set a value for a computed property
- Computed properties can use the following code shorthand
constructor function
Destructors
Swift-Initial (3)