Swift 2.0'inout'函数参数和计算属性 [英] Swift 2.0 'inout' function parameters and computed properties

查看:130
本文介绍了Swift 2.0'inout'函数参数和计算属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在测试Swift 2.0 beta,并且发现了奇怪的行为.这是示例代码:

I'm testing Swift 2.0 beta right now and have found strange behaviour. Here is a sample code:

private func someFunc(inout someString: String) {
    print("Inside \'someFunc()\'")

    print(someString)
    someString = "Some another string"
}

private var someAncillaryInt = 42

print(someAncillaryInt)

private var someString: String {
    get {
        print("Inside \'getter\'")
    
        return "Some string"
    }
    set {
        print("Inside \'setter\'")
        someAncillaryInt = 24
    }
}

someFunc(&someString)
print(someAncillaryInt)

输出:

42

在获取器"内部

在"someFunc()"内部

Inside 'someFunc()'

一些字符串

在设置者"内部

24

我不明白为什么在someFunc()中打印someString时为什么没有调用 getter 以及为什么在someFunc()someString一起传递时为什么会调用它.

I don't understand why wasn't getter called while printing someString inside someFunc() and why was it when someFunc() got passed with someString.

可以假设我还不了解 inout 参数的复杂性,并且在传递 inout 参数的属性后,其计算的属性不再是em,即已计算",但是为什么当我们将另一个值设置为someString时又调用了'setter'?

One can assume that I don't understand intricacies of inout parameters yet and after being passed as inout parameter computed property stops being, em, "computed", but why then was 'setter' called when we set another value to someString?

谢谢!

UPD :我在下面添加了答案.

UPD: I added answer below.

更新2015年11月18日:Apple已更新其

UPDATE 18/11/2015: Apple has updated their manual with detailed explanation of how inout params work.

推荐答案

您的困惑可能是由于同时选择someString和 全局变量的名称,以及 someFunc()功能.

Your confusion might be caused by choosing someString both as the name of a global variable, and as the name of a parameter of the someFunc() function.

print(someString)打印 (本地)函数参数的值,这是完全不相关的 (并隐藏)全局someString变量.

print(someString) inside someFunc() prints the value of the (local) function parameter, which is completely unrelated (and hides) the global someString variable.

重命名功能参数变得更容易理解

It becomes easier to understand if you rename the function parameter

private func someFunc(inout localString: String) {
    print("Inside \'someFunc()\'")
    print(localString)
    localString = "Some another string"
}

在语义上是相同的(因此产生相同的输出).

which is semantically identical (and therefore produces the same output).

您可以想到

someFunc(&someString)

如下:

  • someString的值被检索(使用getter方法).
  • 使用本地参数localString执行
  • someFunc() 设置为someString的值.
  • someFunc()返回时,设置someString(使用 设置方法)设置为本地参数(可能已更改)的值 localString.
  • The value of someString is retrieved (using the getter method).
  • someFunc() is executed, with the local parameter localString set to the value of someString.
  • On return from someFunc(), someString is set (using the setter method) to the (possibly changed) value of the local parameter localString.

更多信息,请参见 https://devforums.apple.com/thread/230567从苹果开发者论坛, 例如:

More information can be found in https://devforums.apple.com/thread/230567 from the Apple Developer Forum, for example:

给定getter和setter的保证,inout自然遵循: 当使用inout参数调用函数时,从逻辑上讲,它将调用 在var/subscript上获取并将值复制到堆栈中 临时的,保证具有物理寻址能力.这 临时实体的物理地址传递给的inout参数 功能 ... .被呼叫者确实 它想要的那个存储位置(永远不知道是否 传入的内容是否经过计算).被呼叫者返回时, 调用设置器将值复制回原位.

Given the guarantee of a getter and setter, inout follows naturally: when calling a function with an inout argument, logically it calls the getter on the var/subscript and copies the value into a stack temporary which is guaranteed to have physical addressability. The physical address of the temporary is passed to the inout argument of the function ... . The callee does whatever it wants with that memory location (and never knows whether the thing passed in was computed or not). When the callee returns, the setter is invoked to copy the value back into place.

它还保证属性的获取器/设置器传入/传出 不管它是什么,都会调用一次其getter和setter方法 被调用者确实如此(如果访问者有副作用或 很贵).

It also guarantees that the getter/setter of a property passed inout will have its getter and setter called once regardless of what the callee does (this is important if the accessors have side effects or are expensive).

,但同时也指出,如有必要,避免使用临时副本.

but it is also stated that the temporary copy is avoided if necessary.

这篇关于Swift 2.0'inout'函数参数和计算属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆