i want to traverse through sortDescriptors NSArray and remove an object that doesn't meet a certain criteria. Can someone here please show me how can i do this correctly.
NSFetchRequest* request = [NSFetchRequest fetchRequestWithEntityName:@“CarsInventory”];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@“model” ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]];
for (CarsInventory* carInfo in request.sortDescriptors)
{
if (![self isCarWithin5FileRadius:carInfo.location])
{
[request.sortDescriptors delete: bookInfo]; // CRASH
}
}
I believe you have two problems here:
NSArray
is not mutable, hence you cannot remove items from it. You should convert it to NSMutableArray
. for(int i=0;i<[yourArray count];i++)
, however. try this code
NSFetchRequest* request = [NSFetchRequest fetchRequestWithEntityName:@“CarsInventory”];
NSArray *sortedArray = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@“model” ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]];
NSMutableArray *mutArray=[NSMutableArray arrayWithCapacity:[sortedArray count]];
for (CarsInventory* carInfo in sortedArray)
{
if ([self isCarWithin5FileRadius:carInfo.location])
{
[mutArray addObject:carInfo];// add it to mutable array
}
}
NSLog(@"New Mut Array--%@",mutArray); //log the final list
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.