Question

I'm using JSONModel in my iOS app and i'm facing some warnings, let me explain myself a bit.

Let's say i have this JSONModel

CTVContact.h

@interface CTVContact : JSONModel
@property (nonatomic, strong) NSArray<Optional, CTVPhone> *phone;
@end

CTVContact.m

NSMutableArray *phones = [[NSMutableArray alloc] init];
for(NSString *p in personPhones) {
    CTVPhone *phn = [[CTVPhone alloc] init];
    phn.original = p;
    [phones addObject:phn];
}    
phone = [NSArray arrayWithArray:phones];

Basically it all works like a charm but i get a warning stating the following:

Incompatible pointer types assigning to 'NSArray<Optional,CTVEventParticipant> *' from 'NSArray *'

How can i go around that warning? I can't find the right way to assign all the array values to the phone Array without that warning.

Thanks a lot in advance!

Was it helpful?

Solution

NSArray<Optional, CTVPhone> *phone; defines a variable that takes an array which conforms to 2 protocols. If you try and set that a variable (phone) to an array that doesn't state that it conforms to those protocols then you will get a compile warning.

phone = [NSArray arrayWithArray:phones]; just creates a 'plain' array, with no special protocols implemented. So you get a warning.

Probably the correct thing to do is to remove the protocols from the @property definition. Unless you have an NSArray subclass which conforms to those protocols that you should actually be using...

Alternatively, and assuming that you don't try to call any methods that might be defined in those protocols:

phone = (NSArray <Optional, CTVPhone> *)[NSArray arrayWithArray:phones];

which adds a cast that basically means to the compiler: 'trust me, it's fine'...

OTHER TIPS

It looks like it may be complaining on your last line, since you're passing in an NSMutableArray when NSArray's arrayWithArray method calls for an NSArray. You can get away with this by calling copy on the phones array, as such:

phone = [NSArray arrayWithArray:[phones copy]];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top