Logical branching simple experience
- Periodic summary
Swift
There is no concept in the C language 非零即真
- The specific conditions of judgment must be indicated in the logical judgment
if
The statement condition ()
can be omitted
- But
{}
you can't omit it.
Tri-Mesh
- Periodic summary
Swift
Maintains the 三目
same style as OC
Optional Walkthrough 1
Periodic summary
In Swift
, not all object instantiation methods return values, and in real development you need to be aware of the return type of the instantiated function, for example:
init?(string URLString: String)
If there is an ?
indication that the modification method may not be instantiated to the correct object
- This function returns an object that is called
可选项
, which may have a value, or it may not have a value
- In actual development, it is necessary to judge this object and use it inside the branch
!
to indicate that the object is actually present.
- Compared to
OC
the development, especially in the daily practice, will be given a function to run the value, and in the actual run, once the condition is not satisfied, will flash back, so the user experience will be very bad
Swift
Designers consider that because of the mandatory requirements of the type, it makes the code difficult to read, and therefore provides a workaround
Walkthrough 2
Periodic summary
if let 常量 = 可选构造函数
the method used ensures that the internal constants of the branch must be valued
- And no longer need to use it inside the branch
!
- This is
Swift
a very important use technique in the code.
Tips
- Although the
Swift
method of type checking is provided, the Swift code to be written will need to be 优雅
practiced, otherwise the code with deep branch nesting level can be inadvertently found.
- The relevant
?
and !
choice can be aided by Xcode, but it is highly recommended that each time you encounter a prompt, think more and repeatedly
Walkthrough 3
- Periodic summary
??
is a very useful operator that can be quickly nil
judged
- If the object is
nil
, use the ??
following value instead of the previous nil
value to participate in the calculation
- When used
??
, the entire section needs to use the ()
packaging
- This technique is
UITableView
especially important in the data source approach
swift-Initial (2)