Search code examples
objective-cios6nsmutablearraynsarray

How to fix warning about incompatible pointer types?


I'm getting this warning:

Incompatible pointer types initializing 'NSMutableArray *__strong' with an expression of type 'NSArray *

This is the line of code giving the warning:

    NSMutableArray *apptDataArray = [AppointmentInfo MR_findAllWithPredicate:nsp];

where MR_findAllWithPredicate returns a NSArray. I have been reading all of the postings in SO and Google, but found nothing like this. I think it's fairly simple, but I need help.


Solution

  • NSArray is not an NSMutableArray, thus the pointers are incompatible and calling mutating methods on it will crash. You can go the other way as an NSMutableArray is an NSArray subclass.

    You can make an NSMutableArray from the return value with +[NSMutableArray arrayWithArray:]:

    NSMutableArray *apptDataArray = [NSMutableArray arrayWithArray:[AppointmentInfo MR_findAllWithPredicate:nsp]];
    

    If your method ever returns nil, create an NSArray * variable and check for nil before creating the mutable array.