Flow controls (Control flow)
For cycles for cycling and C language, OC usage is very similar, usually for () and for in two kinds. Traversal can use the underscore "_" to access the values slightly.
Switchswitch is much more flexible in swift. Just need to be aware of. Unlike the C language, the switch in Swift does not have an implicit penetration. The expression in each case does not need to write a break, and then jumps out on its own initiative. If you want to keep this match and match it down, use Fallthroughkeyword. Each case must have an expression, and each switch must have the default to match what might not have been expected to happen. The matching in Swift is very flexible, can be a range, a tuple or an enumerated case, and so on.
Switch also supports values binding (value bindings)
In the first two cases, we declare x, y two constants, and if so, we can get the value of the values in the point constants that we declared earlier, or Y, and then do some more. Note that the default statement is not written here. is because the last case is equivalent to default. It includes all the cases, and the Xcode compiler can recognize it. So I did not report the error.
Switch is also able to infer with additional conditions when matching, using the Wherekeyword.
Use a method similar to an SQL statement.
Control transfer
Control transfers are primarily used in scenarios where you need to jump out of this loop or immediately terminate the code of the current Access field.
Use of the Continue,break,fallthrough,returnkeyword.
Continue used to stop this loop iteration
Each time this is matched to a l,o or a space. Proceed to the next for loop at once.
Break is the same as other language usage. Jump out of for,switch,while and other process control statements.
Fallthrough belongs to the run-through. After running in case, statement continues to run down the match.
Tagged statements
The ability to label some process control statements before they can be added. to identify them. Standard notation and how to use the identity statement:
The break here no longer jumps out of this switch, but instead jumps out of the entire while loop. Here Primeloop the label plays a role.
Function
In swift, functions no longer let us use + + to declare a method like OC. Instead, use Func. In Swift. When we call a function we can ignore the return value like any other language.
Here in the second function body. We are not practical to return a value of type int in the first function.
Swift's tuple can also be used as the return value of a function.
Here we get the length of a string and the number of characters that represent the number.
Use a tuple to get its own values.
Func is able to define the parameters of the parameter to be more aware of the meaning of the reference for those who are convenient to use the function. Can be preceded by "#" to indicate the external parameter name.
Here we can see the number of participants in the previous reference when we use the function.
Sometimes we want to assign a default value to a parameter when we define it.
When we assign a value, Swift defaults to the addition of an external parameter name.
The function in the variable parameters Swift also has a variable number of parameters. Note that a function parameter has at most only one variable parameter and must be the last of the tables.
Constant arguments and variable parameters The parameters in Swift are implicitly felt constants. And you cannot make changes to the external values.
A parameter is simply a copy of the value, and the life cycle is also inside the function. We can declare the parameters with Var to indicate that they are variable inside the function.
If you want to keep its changes, use Inoutkeyword. When the value is passed in, the address "&" in the C language is required.
The function type is in Swift. Almost everything is a type. An object to render. Therefore, a function is also a type. In other cases, functions can also be used as parameters, such as the return value type or even nested implementations. The flexible syntax allows us to have a lot of other choices in design.
The Addanother function above uses the INT function type (int, int) as the parameter. The following is the two different implementations that return a function. The following implementations use nested functions.
As if forgetting to say, the function uses, to indicate the return value.
All of the above blog content, welcome errata and discussion.
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
Swift Process Control and functionality