我想遍歷sortDescriptors NSArray並刪除不符合特定條件的對象。請問有人能告訴我如何正確地做到這一點。如何從NSFetchRequest sortDescriptors中刪除對象NSArray
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
}
}
陣列'request.sortDescriptors'doesn't握住你獲取的數據,但只有你添加到它的排序描述符。您需要首先將數據提取到新數組中並遍歷該數組。如果你想刪除對象,你將需要一個'NSMutableArray。但是您必須從商店中刪除它們,而不是從提取數組中刪除它們! – Volker
Volker:謝謝,我不想從商店中刪除它們,只想刪除一些不符合request.sortDescriptors標準的對象,然後使用initWithFetchRequest:request將其加載到NSFetchedResultsController中。刪除對象後,我可以將NSMutableArray分配回request.sortDescriptors嗎? – newdev1
你不能用'NSFetchedResultsController'來做到這一點,因爲這個謂詞不能被轉換成'SQL'語句 –