来自苹果文档:

“上面的每个访问级修改器都可以接受一个参数,该参数由括号中包含的关键字集组成(例如,私有(set))。当您要为变量或下标的设置指定访问级别时,使用此形式的访问级修改器,该访问级别小于或等于变量或下标本身的访问级别,如Geters和Setters中所述。”

摘录自:苹果公司。“快速编程语言。” 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

更多来自文档:

“ NumberFedits属性的访问级别用私有(设置)修饰符标记,以表明该属性应仅在与TrackEdstring结构的定义相同的源文件中进行设置。”

摘录自:苹果公司。“快速编程语言。” ibooks。 https://itun.es/ru/jEUH0.l

enter image description here

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

有帮助吗?

解决方案

你的问题就出在这里:

  set { self.name = "Unknown" }

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

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top