I'm trying to save an NSPredicate to the user defaults and I thought I should encode is like so, since this class conforms to NSSecureCoding:
NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"name == 'test'"];
NSData *filterPredicateData = [NSKeyedArchiver archivedDataWithRootObject:filterPredicate
requiringSecureCoding:YES
error:nil];
I then decode it.
NSPredicate *decodedPredicate = [NSKeyedUnarchiver unarchivedObjectOfClass:NSPredicate.class fromData:filterPredicateData error:nil];
if([filterPredicate isEqualTo:decodedPredicate]) { // YES
someNSArrayController.filterPredicate = filterPredicate; // this works
someNSArrayController.filterPredicate = decodedPredicate; // this throws an exception
[someNSArray filteredArrayUsingPredicate: decodedPredicate]; // this throws an exception
}
I'm getting this exception:
This predicate has evaluation disabled
Notes:
decodedPredicateworks as the predicate of anNSFetchRequestwithout issue.decodedPredicateandfilterPredicatereturn the samepredicateFormat.
Can anyone explain this error?
You need to do
[decodedPredicate allowEvaluation];first before using it.From the doc of
allowEvaluation:Sample tests (if someone wants to verify it):