Itachi
Itachi

Reputation: 6070

Objective-C - Can lightweight generic contain multiple types?

This new language feature comes with iOS9 a few time ago, I knew the basic usage.

@property (nonatomic, strong) NSArray<NSString *> *params;

But I want to define params as

NSArray<NSString * or UIImage *>

, the array contains NSString* or UIImage*, is that possible to define explicitly? typedef?

Thanks for all the tips!

Upvotes: 0

Views: 865

Answers (2)

Fonix
Fonix

Reputation: 11597

I have a feeling this is not possible, cause the compiler has to assume a returned object can be 2 completely different types then which isnt allowed. I think the best you can do is maybe make some kind of container object or struct that holds a string and image and you can query the object to see which one it contains, then make the arrays type the container type. otherwise just use the old NSArray without generics and determine what object it is once its retrieved from the array.

Upvotes: 1

Seslyn
Seslyn

Reputation: 827

I think you want to look into __covariants. Here's a decent post that talks about them, specifically in the comments section.

http://drekka.ghost.io/objective-c-generics/

Upvotes: 1

Related Questions