8

来自苹果文档:

“上面的每个访问级别修饰符都可选择接受一个参数,该参数由括在括号中的关键字 set 组成(例如,private(set))。当您想要为变量或下标的设置器指定小于或等于变量或下标本身的访问级别的访问级别时,请使用这种形式的访问级别修饰符,如 Getters 和 Setters 中所述。”< /p>

摘自:Apple Inc.“Swift 编程语言”。电子书。 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 属性的访问级别用 private(set) 修饰符标记,表示该属性只能在与 TrackedString 结构的定义相同的源文件中设置。”</p>

摘自:Apple Inc.“Swift 编程语言”。电子书。 https://itun.es/ru/jEUH0.l

在此处输入图像描述

但这不是我需要的。是否可以禁止在结构/类之外设置变量 numberOfEdits?

4

1 回答 1

11

你的问题出在这里:

  set { self.name = "Unknown" }

您正在其自己的设置器中设置计算属性的值。这会导致无限递归。不要忘记这是一个计算属性:它实际上没有存储空间。您没有变量“self.name”可以放入任何内容;你只有几个函数来计算它。像这样的计算属性应该使用其他非计算变量进行存储。(顺便说一下,这就是您的结构示例有效的原因:您使用的是具有存储功能的不动产。)

在 Playground 中运行并没有帮助您进行调试。不要误会我的意思:游乐场很棒。但是,在这种情况下,崩溃需要几秒钟的时间,因此崩溃可能不会在您预期的编辑后出现。它也没有向您显示完整的堆栈跟踪(这对于您遇到的问题来说是巨大的,已经在“真实”应用程序中复制了它,并且可能更明显地表明您已经炸毁了堆栈。)当我构建并运行上述作为控制台应用程序,它最终炸毁了堆栈跟踪,深度为 104,832 次调用,其中除了两个之外都是...private_cmd.A.name.setter.... 一点线索:)

于 2014-08-01T09:20:06.160 回答