本文介绍了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)

输出:

在获取器"内部

在"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已更新其手册,其中详细说明了inout参数的工作原理.

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 thename of a global variable, and as the name of a parameter of thesomeFunc() function.

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

print(someString) inside someFunc() prints thevalue 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 localStringset to the value of someString.
  • On return from someFunc(), someString is set (using thesetter method) to the (possibly changed) value of the local parameterlocalString.

更多信息,请参见 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:

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

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

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

10-18 21:41