计算 属性 和 属性 集与 swift 3 中闭包的区别?
Difference between computed property and property set with closure in swift 3?
我已经阅读了这个()有用的问题,但它没有完全回答我想知道的问题。
它回答了以下两者之间的区别:
//closure
var pushBehavior: UIPushBehavior = {
let lazilyCreatedPush = UIPushBehavior()
lazilyCreatedPush.setAngle(50, magnitude: 50)
return lazilyCreatedPush
}()
和
//computed
var pushBehavior: UIPushBehavior {
get{
let lazilyCreatedPush = UIPushBehavior()
lazilyCreatedPush.setAngle(50, magnitude: 50)
return lazilyCreatedPush
}
}
我明白了。现在,如果我们更改第一个,第二个示例中的计算变量与这个有什么区别:
var pushBehavior: UIPushBehavior {
let lazilyCreatedPush = UIPushBehavior()
lazilyCreatedPush.setAngle(50, magnitude: 50)
return lazilyCreatedPush
}
对我来说,这与 get{} 完全一样,但没有 get 关键字。是这样吗?如果是这样,为什么我们有 get 关键字?
没有区别。
You can simplify the declaration of a read-only computed property by removing the get
keyword and its braces.
我已经阅读了这个(
它回答了以下两者之间的区别:
//closure
var pushBehavior: UIPushBehavior = {
let lazilyCreatedPush = UIPushBehavior()
lazilyCreatedPush.setAngle(50, magnitude: 50)
return lazilyCreatedPush
}()
和
//computed
var pushBehavior: UIPushBehavior {
get{
let lazilyCreatedPush = UIPushBehavior()
lazilyCreatedPush.setAngle(50, magnitude: 50)
return lazilyCreatedPush
}
}
我明白了。现在,如果我们更改第一个,第二个示例中的计算变量与这个有什么区别:
var pushBehavior: UIPushBehavior {
let lazilyCreatedPush = UIPushBehavior()
lazilyCreatedPush.setAngle(50, magnitude: 50)
return lazilyCreatedPush
}
对我来说,这与 get{} 完全一样,但没有 get 关键字。是这样吗?如果是这样,为什么我们有 get 关键字?
没有区别。
You can simplify the declaration of a read-only computed property by removing the
get
keyword and its braces.