Properties 在swift中,是否可以从属性声明中分离set、get、willSet和willGet?
我将首先向您展示我正在进行的一个项目中的一些属性Properties 在swift中,是否可以从属性声明中分离set、get、willSet和willGet?,properties,swift,Properties,Swift,我将首先向您展示我正在进行的一个项目中的一些属性 /** Properties **/ var coordinates: Coordinates var text: NSString = "" { didSet { self.needsDisplay = true } } var boxViewGroup: GridBoxViewGroup var isLocked: Bool = false { willSet { if isSelected || !newValue
/** Properties **/
var coordinates: Coordinates
var text: NSString = "" {
didSet {
self.needsDisplay = true
}
}
var boxViewGroup: GridBoxViewGroup
var isLocked: Bool = false {
willSet {
if isSelected || !newValue {
boxViewGroup.selectedBox = nil
}
}
}
var isSelected: Bool {
get {
if boxViewGroup.selectedBox {
return boxViewGroup.selectedBox === self
}
else {return false}
}
}
var invertedFrame: NSRect {
get {
return NSRect(x: frame.origin.x,
y: superview.bounds.height - frame.origin.y,
width: bounds.size.width,
height: bounds.size.height)
}
set {
self.frame = NSRect(x: newValue.origin.x,
y: superview.bounds.height - newValue.origin.y - newValue.height,
width: newValue.width,
height: newValue.height)
}
}
看起来有点乱。所以我的问题是,有没有可能把get、set、willGet和willSet方法放在一个单独的位置,这样我的属性声明就可以像这样
var coordinates: Coordinates
var boxViewGroup: GridBoxViewGroup
var isSelected: Bool
var invertedFrame: NSRect
像这样,我实际上可以告诉你有什么属性。可以分为两类。您可以重写子类中的属性声明并添加属性观察员
class DataA {
var coordinates: Coordinates
var boxViewGroup: GridBoxViewGroup
var isSelected: Bool
var invertedFrame: NSRect
}
class A : DataA {
override var coordinates: Coordinates {
didSet {
//add code
}
willSet(newValue) {
//add code
}
}
}
阅读苹果文档中的更多信息