Could you briefly explain what @objc
and dynamic
mean in Swift 4 using Xcode 9.x?
With tries and errors and following articles in the stackoverflow, I have eventually achieved this snippet to work. But I would like to know a little bit about those magical keywords.
class SampleViewController: NSViewController {
@objc class Parameters : NSObject {
@objc dynamic var value1: Double = 0 // bound to Value of a NSTextfield with NumberFormatter
@objc dynamic var value2: Double = 0 // as "parameters.value1" for the Model Key Path
}
@objc dynamic var parameters = Parameters()
@objc dynamic var value3: Double { // in the similar way as "value3" for the Model Key Path
get {
return parameters.value1 + parameters.value2
}
}
override class func keyPathsForValuesAffectingValue(forKey key: String) -> Set<String> {
switch key {
case "value3" :
return Set(["parameters.value1", "parameters.value2"])
default:
return super.keyPathsForValuesAffectingValue(forKey: key)
}
}
}