So, obviously, after WWDC I'm playing with new stuff presented during last week. As you know Apple introduced generics to the world of Objective-C
Note: This answer is somehow follow-up to this question: Are there strongly-typed collections in Objective-C?
I tried this code in method, works great
NSMutableArray<NSString*> *array = [[NSMutableArray alloc] init];
[array addObject:@""];
[array addObject:@(54)];Incompatible pointer types sending 'NSNumber *' to parameter of type 'NSString * __nonnull'
// Great, generics works as expected.
However I also have method I want to transform to generics
In header file:
- (NSArray <NSString*> *)objectsToSearch;
Implementation:
- (NSArray <NSString*> *)objectsToSearch
{
NSString *first = @"1";
NSString *second = @"2";
NSString *third = @"3";
NSNumber *test = @(55);
return @[first, second, third, test]; // No-error!!!
}
Am I doing something wrong or Clang does not support generics + literals or there is something else I'm missing?
I've just been diagnosing this some more and I don't think this is a bug. The following code shows a variety of options and why each will or will not compile. Note: This is based on my guesses as to how things work. It may be different to how Apple would explain it.
Hope this clarifies things for people. Feel free to cut and paste into a unit test and try it out for yourself.