Reputation: 5101
Following method is showing me a warning, but the app is executing as expected. Please could you check the code and tell me what is wrong there? Only if this important to the app, if the warning is not dangering the app, then tell me if I could let this as it is...thank you
The warning is : Incompatible pointer types assigning to 'NSMutableArray *' from 'NSArray *'
at the method definition.
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
self.searchResults = [[self.fetchedResultsController fetchedObjects] filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings)
{
ToDoItem * item = evaluatedObject;
NSString* name = item.todoName;
//searchText having length < 3 should not be considered
if (!!searchText && [searchText length] < 3) {
return YES;
}
if ([scope isEqualToString:@"All"] || [name isEqualToString:scope]) {
return ([name rangeOfString:searchText].location != NSNotFound);
}
return NO; //if nothing matches
}]];
}
Upvotes: 0
Views: 62
Reputation: 62062
self.searchResults = [[[self.fetchedResultsController fetchedObjects]
filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:
^BOOL(id evaluatedObject, NSDictionary *bindings) mutableCopy];
mutableCopy
is a method on many objects for which a mutable
and immutable
version exists. In the case of things like NSArray
, NSString
, NSData
, etc, calling mutableCopy
on one of these instances will return a mutable
version containing the same contents as the original object you called the method on.
For example,
NSArray *immutableArray = [NSArray arrayWithObjects:@"foo",@"bar"];
NSMutableArray *mutableArray = [immutableArray mutableCopy];
However, if you don't intend for searchResults
to be an NSMutableArray
, you should change it's declaration:
@property (nonatomic,strong) NSArray *searchResults
If you don't intend it to be mutable, it should be declared as immutable.
Given your claim that the warning is not effecting the performance of your app, my best guess is that the proper solution would be changing searchResults
from NSMutableArray
to NSArray
.
Upvotes: 1
Reputation: 539975
filteredArrayUsingPredicate
returns an immutable NSArray
,
and you seem to have declared searchResults
as NSMutableArray
.
So either
searchResults
to NSArray
, or mutableCopy
before assigning it.The proper solution depends on whether you need to modify the searchResults
later or not.
Upvotes: 1