Computed read-only property vs function in Swift

后端 未结 10 930
忘了有多久
忘了有多久 2020-12-04 11:00

In the Introduction to Swift WWDC session, a read-only property description is demonstrated:

class Vehicle {
    var numberOfWheels = 0
    var          


        
10条回答
  •  盖世英雄少女心
    2020-12-04 11:09

    Since the runtime is the same, this question applies to Objective-C as well. I'd say, with properties you get

    • a possibility of adding a setter in a subclass, making the property readwrite
    • an ability to use KVO/didSet for change notifications
    • more generally, you can pass property to methods that expect key paths, e.g. fetch request sorting

    As for something specific to Swift, the only example I have is that you can use @lazy for a property.

提交回复
热议问题