Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 781
Akshay Avatar asked Oct 15 '25 18:10

Akshay


2 Answers

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+.

like image 103
user28434'mstep Avatar answered Oct 18 '25 20:10

user28434'mstep


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.

like image 27
Akshay Avatar answered Oct 18 '25 19:10

Akshay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!