I'm finishing a port for a project that was written in Swift to support Objective-C. A lot of the project was written to support Objective-C but not the properties on a particular class.
This is the property:
open var remainingTime: ( hours: Int, minutes: Int, seconds: Int)?
I'm guessing I can't just add @objc
to this because "hours","minutes","seconds" are objects. How do I make this property visible to my Objective-C project?
This error occurred in project when I tried to observe an Enum via KVO. My enum looked like this:
enum EnumName: String {
case one = "One"
case two = "Two"
}
In case you are struggling to observe it, this workaround helped solve my issue.
@objc dynamic var observable: String?
create your enum instance like this:
private var _enumName: EnumName? {
didSet {
observable = _enumName!.rawValue
}
}
private var _enumName: EnumName?
private let _instance = ObservableClass()
create
private var _enumObserver: NSKeyValueObservation = _instance.observe(\.observable, options: .new, changeHandler: { [weak self] (_, value) in
guard let newValue = value.newValue else { return }
self?._enumName = EnumName(rawValue: period)!
})
Than's it. Now each time you change the _enumName
in the observable class, an appropriate instance on the observer class will be immediately updated as well.
This is of course an oversimplified implementation, but it should give you an idea of how to observe KVO-incompatible properties.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With