Getter vs Computed property. What would warrant using one of these approaches over the other?

前端 未结 2 990
我在风中等你
我在风中等你 2020-12-21 16:14

Take a look at this variable

var username: String {
    get {
        return self.twitterAccount.valueForKey(\"username\") as! String
    }
}
相关标签:
2条回答
  • 2020-12-21 17:14

    Both forms are exactly the same thing, a read-only computed property.

    From the documentation:

    You can simplify the declaration of a read-only computed property by removing the get keyword and its braces.

    0 讨论(0)
  • 2020-12-21 17:15

    If you only have a getter you can do this simplification. But if you also have a setter you have to use the former version in order to separate both.

    0 讨论(0)
提交回复
热议问题