My blog
In Swift, the parameter of a function is constant by default and cannot be modified. If I need to modify a parameter of the function inside the function or return the content through the parameter, I need to use the inout keyword to modify the parameter and add & before the parameter when calling.
If a teacher needs to add 30 points to a student’s written score, he/she needs to use the inOUT parameter:
func configStudent(_ fraction: inout Int) -> Int {
return fraction + 30
}
var num = 53
configStudent(&num)
Copy the code
The &num notation, in other C family languages such as C/C++ and Objective-C, is generally thought of as passing Pointers to variables. In fact, THAT’s what I thought until I read the functions section of the official documentation and learned that the inout keyword is not used that way.
1. Inout is just in-out
Here’s what the official document says:
You write an in-out parameter by placing the inout keyword right before a parameter's type Value that's passed in to the function, is modified by the function, and is passed back out of the function to replace the original value.
The documentation makes it very clear that inout is passing parameters to a function and then replacing the initial values after the function changes them. In other words, inout simply means in-out and has no other meaning.
2. Use of Inout
2.1. Variables are allowed, but constants are not
- use
inout
You can only modify variables
func configStudent(_ fraction: inout Int) -> Int {
return fraction + 30
}
let num = 53
configStudent(&num)
Copy the code
Cannot pass immutable value as inout argument: ‘num’ is a ‘let’ constant
Var num = 53
var num = 53
configStudent(&num)
Copy the code
2.2. Constants in mutable arrays can also be usedinout
But again, what if I modify an argument in a mutable array?
var testArray = [1.2.3.4.5.6]
testArray[1] = configStudent(&testArray[1]) // [1, 32, 3, 4, 5, 6]
Copy the code
Compile code, running properly, indicating that subscript operations in arrays can also use inout to modify parameters.
2.3. Use custom type attributesinout
The keyword
For example, NOW I want to record the location of students when they sign up for class, and then make a correction after obtaining the location information of students, using the custom type, the code is as follows:
struct Location {
var lat: Double
var lon: Double
}
func configLocation(_ num: inout Double) -> String {
return "\(num + 0.01)"
}
/ / call
var location = Location(lat: 37.33020, lon: -122.024348)
configLocation(&location.lat)
Copy the code
Compile the code, running fine.
Using the above code, I not only need to know the latitude and longitude, but also want to know the student’s usual score. Generally, by default, the teacher will set the usual score for all students to be 30. The new fraction attribute is used as the usual score, and the code is as follows:
struct Location {
var lat: Double
var lon: Double
var fraction: Int {
return 30}}func configFraction(_ fraction: inout Int) -> Int {
return fraction + 30
}
configFraction(&location.fraction)
Copy the code
Cannot pass immutable value as inout argument: ‘fraction’ is a get-only property, meaning that the inout parameter cannot be used for read-only attributes of a custom class.
The final conclusion is that attributes of custom types have both get and set methods and can also be used as inout parameters. Read-only attributes in custom types cannot use the inout parameter
This article is mainly about some of my misunderstandings about inout modifier parameters, and the use of inout modifier parameters. Please point out any mistakes.
Reference: The Swift Programming Language: Functions
In this paper, the code