What does an ampersand (&) mean in the Swift language?

前端 未结 5 447
没有蜡笔的小新
没有蜡笔的小新 2020-12-01 02:29

I know about the ampersand as a bit operation but sometimes I see it in front of variable names. What does putting an & in front of variables do?

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-01 03:11

    If you put & before a variable in a function, that means this variable is inout variable.

    @Icaro already described what it means, I will just give an example to illustrate the difference between inout variables and in variables:

    func majec(inout xValue:Int, var yValue:Int) {
        xValue = 100
        yValue = 200
    }
    
    var xValue = 33
    var yValue = 33
    majec(&xValue, yValue: yValue)
    xValue //100
    yValue //33
    

提交回复
热议问题