IPhone: warning pops up when reverse NSMutablearray

I need a reverse (sorting descending) NSMutablearray. I tried the following code and it works but got a warning. How can I remove the warning?

NSMutablearray *resStatusArray;

[resStatusArray sortUsingSelector:@selector(compare:)];

    resStatusArray=[[resStatusArray reverseObjectEnumerator] allObjects];

      

Warning:Incompitable pointer types assigning to 'NSMutablearray*_strong' from ' NSarray *'

      

+3


source to share


4 answers


user1118321 is correct in the explanation. To fix this, you must create a new mutable array. I suspect it is safer than casting.



resStatusArray = [NSMutableArray arrayWithArray:[[resStatusArray reverseObjectEnumerator] allObjects]];

      

+7


source


The method allObjects

returns NSArray*

, not NSMutableArray*

, which is why the compiler tells you your variable assignment is NSMutableArray*

wrong.



+4


source


If you are sure of what you are doing, you can use a cast:

resStatusArray=(NSMutableArray*_strong)[[resStatusArray reverseObjectEnumerator] allObjects];

      

However, I doubt this is semantically correct.

0


source


Set it equal:

[NSMutableArray arrayWithArray:[[arrayNameToReverse reverseObjectEnumerator] allObjects]];

      

This essentially creates a new array from the old modified array, but vice versa. The reason it works is because NSMutableArray is a subclass of NSArray. So your code to modify your array will look like this:

resStatusArray = [NSMutableArray arrayWithArray:[[resStatusArray reverseObjectEnumerator] allObjects]];

      

0


source







All Articles