Objective-C generics not working for methods? (Xcode 7 Beta (build: 7A120f)) -


so, obviously, after wwdc i'm playing new stuff presented during last week. know apple introduced generics world of objective-c

note: answer somehow follow-up question: are there strongly-typed collections in objective-c?

i tried code in method, works great

nsmutablearray<nsstring*> *array = [[nsmutablearray alloc] init];  [array addobject:@""]; [array addobject:@(54)];incompatible pointer types sending 'nsnumber *' parameter of type 'nsstring * __nonnull' // great, generics works expected. 

however have method want transform 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 doing wrong or clang not support generics + literals or there else i'm missing?

i've been diagnosing more , don't think bug. following code shows variety of options , why each or not compile. note: based on guesses how things work. may different how apple explain it.

#pragma clang diagnostic push #pragma clang diagnostic ignored "-wunused-variable" -(void) testgenericarrays {      nsstring *astring = @"abc";     nsnumber *anumber = @(55);      nsarray<nsstring *> *arr1  = @[astring, anumber];     // compiles because rhs un-typed array @ compilation time.      nsarray<nsstring *> *arr2  = @[astring, @(20)];     // compiles because rhs un-typed array @ compilation time.      nsarray<nsstring *> *arr3 = [nsarray<nsstring *> arraywithobjects:astring, anumber, @(20), nil];     // compiles because type erasure arraywithobjects types first argument nsstring.     // rest of arguments valist not checked during header processing.      nsarray<nsstring *> *arr4 = [nsarray<nsstring *> arraywithobjects:@(20), nil]; // <- error!     nsarray<nsstring *> *arr5 = [nsarray<nsstring *> arraywithobjects:anumber, nil]; // <- error!     // neither of these 2 compile because first argument nsnumber , checked.      nsarray<nsstring *> *arr6 = [nsarray<nsstring *> arraywithobject:anumber]; // <- error!     // doesn't compile because argument checked during header processing.      nsarray<nsstring *> *arr7 = [nsarray arraywithobject:anumber];     // compiles because rhs un-typed array @ compilation time.      nsmutablearray<nsstring *> *arr8 = [[nsmutablearray alloc] init];     [arr8 addobject:anumber]; // <- error!     // doesn't compile because lhs typed array , adding it. } #pragma clang diagnostic pop 

hope clarifies things people. feel free cut , paste unit test , try out yourself.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -