为啥 private(set) 在 Swift 中不起作用?

Posted

技术标签:

【中文标题】为啥 private(set) 在 Swift 中不起作用?【英文标题】:Why private(set) is not working in Swift?为什么 private(set) 在 Swift 中不起作用? 【发布时间】:2014-08-01 07:55:28 【问题描述】:

来自 Apple 文档:

“上面的每个访问级别修饰符可选地接受一个 参数,由括号中的关键字集组成 (例如,私有(集))。使用这种形式的访问级别 修饰符,当您想为 a 的 setter 指定访问级别时 小于或等于访问级别的变量或下标 变量或下标本身,如 Getters 和 二传手。”

摘自: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 结构相同的源文件中 定义。”

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

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

【问题讨论】:

您确定是访问控制导致了问题吗?您似乎在自己的设置器中设置变量的值,我认为这会导致无休止的递归。 (你设置名称,调用你的设置器,设置名称,调用你的设置器......) 标题令人困惑。这听起来像一篇文章的标题,它解释了为什么 private(set) 在 Swift 中不起作用。请让我建议将标题更改为“在我的特殊情况下,为什么不能在 Swift 中使用私有(设置)?” 【参考方案1】:

你的问题在这里:

  set  self.name = "Unknown" 

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

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

【讨论】:

以上是关于为啥 private(set) 在 Swift 中不起作用?的主要内容,如果未能解决你的问题,请参考以下文章

swift当某个属性要被外部访问但不希望被外部修改(内部可改)

斯威夫特:这个错误:'private(set)'修饰符不能应用于只读属性是啥意思?

java中get和set方法为啥get在前

swift 基础小结02 -- VFL约束属性的get和set方法懒加载

scala设计者为啥要提供package object

Swift-Swift - 访问控制(private,internal,public)