環境: iOS SDK9
在你想比對NSNumber時,從JSON轉出來的數值出現了NSTaggedPointerString,你會比對不到,於是得到nil.
- (Visitor *)visitorByID:(NSNumber *)uid {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Visitor" inManagedObjectContext:self.managedObjectContext]];
if ([uid isKindOfClass:[NSNumber class]]) {
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"self.visitor_id == %@", uid]];
} else {
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
NSNumber *numUid = [numberFormatter numberFromString:(NSString *)uid];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"self.visitor_id == %@", numUid]];
}
NSError * error = nil;
NSArray * result = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (error!=nil) {
SOLogError(@"%s[%d:%s]: %@", __FILE__, __LINE__, __FUNCTION__,error.localizedDescription);
}
}
解法:
把NSTaggedPointerString再轉回NSNumber一次
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
NSNumber *numUid = [numberFormatter numberFromString:(NSString *)uid];
沒有留言:
張貼留言