One, underline
In Objective-c, the underscore (_) is not much of a function, generally used to define variable names, such as the definition of member variables int _age; But in Swift, it has a very powerful effect.
1. Only as a marker
var money = 1_000_000
The variable money still represents the number 1000000, but it looks clearer and the numbers seem to be spaced out.
2. Any match
For example, determine if the point (in) is within a certain range. We can use the following notation
As you can see, case (_,0) and case (0,_) represent the x and Y axes respectively, and here "_" represents all the points above the x or Y axis.
3. Negligible effect
For example, in the For loop, you only need to execute the contents of the inside, but it has nothing to do with the loop variable.
For _ in 1...8 { println ("Why")}
For Another example, omit the external parameter name and define a constructor for the default parameter value first
Func addstudent (name:string, age:int =) { println ("Add Student: name=\ (name), age=\ (age)")}addstudent ("Jack", age:20)
you can see that the age parameter has a default parameter value of 20, so at the time of invocation, there is an age hint in the calling method.
Below, we try to use _ to ignore, the code is as follows:
Func AddStudent2 (name:string, _ Age:int =) { println ("Add Student: name=\ (name), age=\ (age)")}addstudent2 ("Rose", 18)
As you can see, there is no need to have the age prompt for this call.
Note: For definitions of functions, methods, external parameter names, and so on, I'll cover them in more detail in the next section.
Second, switch usage
1. Basic usage, the observation notation, the switch use in Swift does not need the break keyword.
2. Multi-conditional matching, it can be seen that the wording and objective-c is also different, in the objective-c, the multi-conditional matching is a few case parallel write, and in Swift, only a case and the conditions between the "," separated.
3. Scope matching, in fact, "..." in the usage, the previous section, I have already explained, here is not explained.
4. Tuple matching (see underscore usage 2)
5. If the condition is too slight, the WHERE keyword is introduced and the condition is too small with switch.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Swift: Summary of underline and switch usage