Passing references to parameters
A class is a reference type, and other data types such as Integer, float, Boolean, character, string, tuple, collection, enumeration, and struct are all value types.
Sometimes it is possible to pass a value type parameter as a reference, which is achievable, and theinout keyword provided by Swift can be implemented. Look at one of the following examples:
[HTML]View PlainCopyprint?
- Func Increment (inout value:double, amount:double = 1.0) {
- Value + = Amount
- }
- var value: Double = 10.0
- Increment (&value)
- Print (value)
- Increment (&value, amount:100.0)
- Print (value)
Code Increment (&value) is the call function increment, the increment is the default value, where &value (add & symbol before the variable , take out the value address) is the way to pass the reference , when defining a function, the parameter identification corresponds to the inout.
Code Increment (&value,amount:100.0) is also called function increment, and the increase is 100.0.
The above code output results are as follows:
11.0
111.0
&-in Swift