swift - Swift 语言中如何使用私有(private)属性?

标签 swift

<分区>

似乎在 swift 语言中没有 private 或 public 关键字。 那么拥有私有(private)属性(property)是完全可能的吗?

顺便说一句,根据我目前的观察,Swift 与 Typescript 非常相似。

最佳答案

更新:

从 Xcode 6 beta 4 开始,Swift 具有访问控制 - https://developer.apple.com/swift/blog/?id=5


Swift 还没有访问修饰符。但是there are plans to add them in the future

上面链接的线程中也提到了一些解决方法,比如使用嵌套类:

import Foundation

class KSPoint {

    /*!
     * Inner class to hide the helper functions from codesense.
     */
    class _KSPointInner {
        class func distance(point p1 : KSPoint, toPoint p2 : KSPoint) -> Double {
            return sqrt(pow(Double(p2.x - p1.x), 2) + pow(Double(p2.y - p1.y), 2))
        }
    }

    var x : Int

    var y : Int

    init(x : Int = 0, y : Int = 0) {
        self.x = x
        self.y = y
    }

    func distance(point : KSPoint, toPoint : KSPoint) -> Double {
        return _KSPointInner.distance(point: point, toPoint: toPoint)
    }
}

关于swift - Swift 语言中如何使用私有(private)属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24021802/

相关文章:

ios - MyViewController 是否在窗口层次结构中?

swift - 在 swift 3 中使用扩展时使用未解析的标识符

ios - 栏按钮项目无法执行操作 Swift

ios - 快速将数据传递给 tableView 单元格

ios - 解析 Swift 更新错误

swift - 完成处理程序没有按预期的 Swift 被调用

ios - Swift:NSUserDefaults 没有立即设置

ios - 索要号码功能错误 canOpenURL : failed for URL: "tel://0478733797" - error: "This app is not allowed to query for scheme tel"

swift - [Swift]annotationview 如何始终显示标注

ios - 如何使用 Realm 移动平台与多个用户共享 Realm 或 Realm 对象?