0

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!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Itachi
  • 5,777
  • 2
  • 37
  • 69
  • 1
    Why would you want this instead of using a supertype like `id` or `NSObject *`? What can you safely do with a value of type `NSString * or UIImage *` other than what you can do with a supertype like `NSObject *`? – newacct Jan 07 '16 at 01:34
  • I know what you mean, my purpose is making the caller known the public params only allow the NSString* and UIImage* items, for static checker only. @newacct – Itachi Jan 07 '16 at 08:30
  • E.g. you get warnings if you try to pass wrong object to the array. So case with more then one data type is relevant. – Jakub Truhlář Sep 01 '16 at 16:16

2 Answers2

1

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/

Seslyn
  • 807
  • 10
  • 19
1

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.

Fonix
  • 11,447
  • 3
  • 45
  • 74