Reputation: 832
In Swift, we can create a generic property like:
class MyClass<T: MyOtherType where T: MyProtocol> {
var property: T
}
How is it possible in Objective-C?
Upvotes: 4
Views: 2372
Reputation: 832
Here is answer.
@interface myParentView< T: parentModel*> :UIView
@property T myObject; // myObject is object of parentModel
@end
In all subclass:
@interface myChildViewOne :myParentView<childModel>
// Now myObject is object of childModel
@end
Obj C has complicated syntax, But we can achieve generic property like above.
Upvotes: 3
Reputation: 6600
Assuming:
@interface MyOtherType : NSObject
// Some code
@end
@protocol MyProtocol <NSObject>
// Some code
@end
You can do this:
@interface MyClass : NSObject
@property MyOtherType <MyProtocol> * property;
@end
Syntax is Class <Protocol>
.
It would be actually something like Class & Protocol
type in Swift 4+
.
Upvotes: 4