I'm currently working on protocols for my objects which inherits from Realm's Object. Inside my objects I have variables and these variables are marked as @objc dynamic
@objc dynamic var title: String = ""
Now imagine situation that I have more similar objects with same variable title. I want to create protocol for them since I want to have just one generics method for changing title of object.
So, I created protocol with title variable marked as @objc dynamic with the expectation that this is how it works
protocol Titleable: class {
@objc dynamic var title: String { get set }
}
... this didn't work out and I received actually two errors.
One about marking variable as @objc
@objc can only be used with members of classes, @objc protocols, and concrete extensions of classes
... this I could solve by marking protocol as @objc.
However I still had error connected with dynamic keyword
Only members of classes may be dynamic
... I thought that when I constrained protocol for classes, it should be alright, but... it wasn't.
I somehow solved it by removing @objc as well as dynamic keywords
protocol Titleable: class {
var title: String { get set }
}
... that works. I'm able to mark variable as @objc dynamic in class where I implement this protocol.
class Item: Object, Titleable {
@objc dynamic var title: String = ""
}
However, I'm not sure why this works and why marking variable as dynamic inside protocol declaration doesn't. I would appreciate any explanation.