swift 关键字inout

来源:互联网 发布:程序员未来的发展方向 编辑:程序博客网 时间:2024/05/23 00:26

关键字inout:当我们需要通过一个函数来改变函数外面变量的值(以引用方式传递)。
通俗的讲:就是使用inout关键字修饰的值,在接下来的方法中可以修改。
代码如下:

    var test1: CGFloat = 50        func testMethod() {        print(test1) // 打印出来是什么???        print("This is a test number: \(test1)")        addTestMethod(test2: &test1)        print("This is a changed number: \(test1)")    }        func addTestMethod(test2:inout CGFloat, test3: CGFloat = 10) {        test2 += test3    }

打印出来结果就是:

50.0This is a test number: 50.0This is a changed number: 60.0