Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective c protocol generics

Can Objective-C protocol be generic?

Following this tutorial, I'm basically looking for something like that:

@protocol ItemsStore<__covariant ObjectType> <NSObject>

-(NSArray <ObjectType> *)items;

@end

Which is a generic protocol for some ObjectType that "implements" ("inherits") another protocol NSObject

like image 646
mllm Avatar asked Feb 03 '16 17:02

mllm


2 Answers

As @rmaddy suggested, and as referred to this questions, it is NOT possible. Shame, moving to Swift then...

like image 109
mllm Avatar answered Oct 11 '22 18:10

mllm


Maybe you could just redefine it as generic in the interface.

@protocol ItemsStore <NSObject>

- (NSArray *)items;

@end

@interface MyItemsStore<ObjectType> : NSObject <ItemsStore>

- (NSArray <ObjectType> *)items;

@end

This seems an unlikely scenario though. You might be better just defining the type of the items in each subclass. Like what Apple do with NSFetchRequest in their core data model generation.

like image 42
malhal Avatar answered Oct 11 '22 16:10

malhal