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

前端 未结 5 467
没有蜡笔的小新
没有蜡笔的小新 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:16

    It means that it is an in-out variable. You can do something directly with that variable. It is passed by address, not as a copy.

    For example:

    var temp = 10
    func add(inout a: Int){
        a++
    }
    add(inout:&temp)
    temp // 11
    

提交回复
热议问题