为什么私有(set)在Swift中不起作用?

时间:2014-08-01 07:55:29

标签: ios swift swift-playground

来自Apple文档:

  

“上面的每个访问级别修饰符都可选择接受一个   参数,由括在括号中的关键字组成   (例如,私人(集))。使用此形式的访问级别   当您要为a的setter指定访问级别时的修饰符   变量或下标小于或等于访问级别   变量或下标本身,如Getters和   塞特斯“。

     

摘自:Apple Inc.“The Swift Programming Language。”iBooks。   https://itun.es/ru/jEUH0.l

我尝试在Playground中测试的一个例子:

import UIKit


class A {
  private(set) var name: String {
  get { return "Hello, \(self.name)" }
  set { self.name = "Unknown" }
  }

  init(_ name:String) {
    self.name = name
  }
}

let a = A("Andrew")
a.name = "Hello"

我在控制台输出中收到的错误:

Playground execution failed: error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=2, address=0x7fff5056eff8).
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.
* thread #1: tid = 0xea721, 0x00000001104f308c libsystem_pthread.dylib`__mtx_droplock + 222, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=2, address=0x7fff5056eff8)
  * frame #0: 0x00000001104f308c libsystem_pthread.dylib`__mtx_droplock + 222
    frame #1: 0x00000001104f2f07 libsystem_pthread.dylib`pthread_mutex_unlock + 68
    frame #2: 0x000000010ffbd2b5 libc++.1.dylib`std::__1::mutex::unlock() + 9
    frame #3: 0x000000010f040b94 libswift_stdlib_core.dylib`swift_getGenericMetadata + 260
    frame #4: 0x000000010ef28a24 libswift_stdlib_core.dylib`Swift.IndexingGenerator.init <A : Swift._Collection>(Swift.IndexingGenerator<A>.Type)(A) -> Swift.IndexingGenerator<A> + 164
    frame #5: 0x000000010ef55f1a libswift_stdlib_core.dylib`protocol witness for Swift._Sequence_.generate <A : Swift._Sequence_>(@inout Swift._Sequence_.Self)() -> Swift._Sequence_.Self.GeneratorType in conformance Swift._ContiguousArrayBuffer : Swift._Sequence_ + 154
    frame #6: 0x000000010ef284d5 libswift_stdlib_core.dylib`Swift._copyCollectionToNativeArrayBuffer <A : protocol<Swift._Collection, Swift._Sequence_>>(A) -> Swift._ContiguousArrayBuffer<A.GeneratorType.Element> + 1061
    frame #7: 0x000000010ef40281 libswift_stdlib_core.dylib`Swift.Array.convertFromArrayLiteral <A>(Swift.Array<A>.Type)(Swift.Array<A>...) -> Swift.Array<A> + 641
    frame #8: 0x000000010f1eaae4 PlaygroundLogger`Swift.UInt64.toBytes (Swift.UInt64)() -> Swift.Array<Swift.UInt8> + 292
    frame #9: 0x000000010f1eb6a4 PlaygroundLogger`protocol witness for PlaygroundLogger.ToBytes.toBytes <A : PlaygroundLogger.ToBytes>(@inout PlaygroundLogger.ToBytes.Self)() -> Swift.Array<Swift.UInt8> in conformance Swift.UInt64 : PlaygroundLogger.ToBytes + 20
    frame #10: 0x000000010f1dbe3d PlaygroundLogger`PlaygroundLogger.BytesStream.write (PlaygroundLogger.BytesStream)(PlaygroundLogger.ToBytes) -> PlaygroundLogger.BytesStream + 77
    frame #11: 0x000000010f1dbd74 PlaygroundLogger`PlaygroundLogger.BytesStream.write (PlaygroundLogger.BytesStream)(Swift.String) -> PlaygroundLogger.BytesStream + 164
    frame #12: 0x000000010f20f04b PlaygroundLogger`PlaygroundLogger.PlaygroundWriter.encode_config_info (PlaygroundLogger.PlaygroundWriter)() -> () + 203
    frame #13: 0x000000010f20f2bf PlaygroundLogger`PlaygroundLogger.PlaygroundWriter.encode_header (PlaygroundLogger.PlaygroundWriter)() -> () + 127
    frame #14: 0x000000010f20ecda PlaygroundLogger`PlaygroundLogger.PlaygroundScopeWriter.encode_scope_event (PlaygroundLogger.PlaygroundScopeWriter)(PlaygroundLogger.ScopeEvent) -> () + 58
    frame #15: 0x000000010f1eb997 PlaygroundLogger`playground_log_scope_entry + 87
    frame #16: 0x000000011ae20771

我做错了什么?我错过了什么吗?

PS1

这个例子很好用:

struct TrackedString {
  private(set) var numberOfEdits = 0
  var value: String = "" {
  didSet {
    numberOfEdits++
  }
  }
}

var stringToEdit = TrackedString()
stringToEdit.value = "Hello"
stringToEdit
stringToEdit.numberOfEdits += 10
stringToEdit

更多来自文档:

  

“numberOfEdits属性的访问级别标有a   private(set)修饰符,表示该属性应该是可设置的   只能在与TrackedString结构相同的源文件中   定义“。

     

摘自:Apple Inc.“The Swift Programming Language。”iBooks。   https://itun.es/ru/jEUH0.l

enter image description here

但那不是我需要的。是否可以禁止在struct / class之外设置变量numberOfEdits?

1 个答案:

答案 0 :(得分:11)

你的问题在于:

  set { self.name = "Unknown" }

您在自己的setter中设置计算属性的值。这会导致无限递归。别忘了这是计算属性:它实际上没有存储空间。你没有变量&#34; self.name&#34;什么东西;你只有几个函数来计算它。像这样的计算属性应该使用其他非计算变量进行存储。 (顺便说一下,这就是你的结构示例有效的原因:你在使用存储的不动产时。)

通过在Playground中运行,您在调试过程中没有得到帮助。不要误会我的意思:游乐场很棒。但是,在这种情况下,它需要花费很多秒才能崩溃,所以崩溃可能不会在编辑后出现。它也没有向你显示一个完整的堆栈跟踪(这对于你正在获得的问题来说是巨大的,在一个真实的应用程序中重现它,并且可能使它变得更加明显当你构建并运行上面的控制台应用程序时,它最终爆炸了104,832个深度的堆栈跟踪,除了其中两个是...private_cmd.A.name.setter...之外。有点线索:))