我有一个 NSArray 包含 NSDictionary 对象。
我需要根据选择的值来过滤对象,所以我创建了一个我想要过滤的值数组,并使用 predicatewithformat 并将其提供给对象数组。
这是一种工作,但奇怪的是,在我知道我应该返回一个空数组的情况下,我得到一个不应该存在的对象。
我已经注销了过滤器值数组的值,我可以清楚地看到它包含一个与对象的 id_str 对应的键,因此不应返回它。
下面是我正在使用的代码,任何我出错的地方都会非常有帮助!
//Create new fetch request
NSFetchRequest *request = [[NSFetchRequest alloc] init];
//Set new predicate to only fetch tweets that have been favourited
NSPredicate *filterFavourite = [NSPredicate predicateWithFormat:@"favouriteTweet == 'YES'"];
//Setup the Request
[request setEntity:[NSEntityDescription entityForName:@"Tweet" inManagedObjectContext:_managedObjectContext]];
//Assign the predicate to the fetch request
[request setPredicate:filterFavourite];
NSError *error = nil;
//Create an array from the returned objects
NSArray *favouriteTweets = [_managedObjectContext executeFetchRequest:request error:&error];
NSAssert2(favouriteTweets != nil && error == nil, @"Error fetching events: %@\n%@", [error localizedDescription], [error userInfo]);
//Create a new array containing just the tweet ID's we are looking for
NSArray *favouriteTweetsID = [favouriteTweets valueForKey:@"tweetID"];
NSLog(@"%@", favouriteTweetsID);
//Create a new predicate which will take our array of tweet ID's
NSPredicate *filterFavouritsearchPredicate = [NSPredicate predicateWithFormat:@"(id_str != %@)" argumentArray:favouriteTweetsID];
//Filter our array of tweet dictionary objects using the array of tweet id's we created
NSArray *filteredTweets = [self.timelineStatuses filteredArrayUsingPredicate:filterFavouritsearchPredicate];
//Send those tweets out to be processed and added to core data
[self processNewTweets:filteredTweets];
NSLog(@"Update Favoutited Tweets: %@", filteredTweets);