Akshay
Akshay

Reputation: 832

How can I create a generic property in Objective-C?

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

Answers (2)

Akshay
Akshay

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

user28434&#39;mstep
user28434&#39;mstep

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

Related Questions