将 属性 public/make 设为只读且可写 Swift 类别?

Make private property public/make readonly one writeable with Swift category?

The question is the same as this one

但我需要 Swift 的解决方案。一般情况下 Swift 是否可以?当然,我的意思是无需创建 Objective C 文件并将其导入 swift 代码

的解决方案

否 - 在 Swift 中不可能 - 至少在当前 Swift 3 版本中不可能。

如果变量被定义为 "private",您甚至不能从扩展访问它,甚至不能通过子class 访问它。

如果您可以将此 "private" 变量设为 "fileprivate" 或 "internal" - 将有可能通过 public 方法公开它们。

这是来自 Apple 的文档: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AccessControl.html

如果您正在使用 Swift 2 - 您应该能够制作 public 函数,该函数将 return 来自主 class.[=11= 的私有变量]