Read-only and non-computed variable properties in Swift

后端 未结 4 1406
感情败类
感情败类 2020-12-23 08:54

I\'m trying to figure out something with the new Apple Swift language. Let\'s say I used to do something like the following in Objective-C. I have readonly prop

4条回答
  •  失恋的感觉
    2020-12-23 09:32

    Simply prefix the property declaration with private(set), like so:

    public private(set) var hours:   UInt = 0
    public private(set) var minutes: UInt = 0
    public private(set) var seconds: UInt = 0
    

    private keeps it local to a source file, while internal keeps it local to the module/project.

    private(set) creates a read-only property, while private sets both, set and get to private.

提交回复
热议问题