Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use generics in Objective C for an app targeting iOS 8?

Is the new Xcode 7 generics feature for Objective C a compiler feature or does it require iOS 9?

Can I target iOS 8, but still use generics?

like image 718
Johan Nordberg Avatar asked Dec 24 '22 14:12

Johan Nordberg


1 Answers

It's a compiler feature. All that happens when you declare e.g. NSArray<NSString *> * is that the compiler will then pretend that instead of - addObject:(id) you've got - addObject:(NSString *), generating appropriate messages if you supply the wrong type of argument.

It's still Objective-C and everything is still all dynamically typed. There's only one implementation of NSArray, which is always used regardless of generics, and is the same as it ever was. No code generation occurs, no dynamic runtime shenanigans occur.

Furthermore this is true of all classes, not merely NSArray. The 'lightweight' in Apple's lightweight generics means you're supplying compile-time hints only.

like image 97
Tommy Avatar answered Dec 28 '22 10:12

Tommy