1

我面臨着使用predicateWithFormat方法創建NSPredicate類型對象的問題。用於電子郵件問題的NSPredicate predicateWithFormat

AppDelegate *appdelegateObj = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 

self.managedObjectContext = [appdelegateObj managedObjectContext]; 
NSString *emailWithBackslashedAtTheRate = [email stringByReplacingOccurrencesOfString:@"@" withString:@"\\@"]; 

NSFetchRequest *fetchRequestObj = [NSFetchRequest fetchRequestWithEntityName:@"Usertable"]; 
NSMutableArray *dataList; 

**NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"email CONTAINS[c] %@",emailWithBackslashedAtTheRate]];** 
[fetchRequestObj setPredicate:predicate]; 

@synchronized(delegate.persistentStoreCoordinator) { 

    NSError *error = nil; 
    NSMutableArray *filteredUser = (NSMutableArray *)[self.managedObjectContext executeFetchRequest:fetchRequestObj error:&error]; 
} 

任何人都可以解釋,什麼是問題?

+0

是什麼通過調用'executeFetchRequest'返回的'error'? –

+0

emailWithBackslashedAtTheRate的格式是什麼?實體中的電子郵件屬性名稱是什麼? –

+0

你能記錄什麼是問題? –

回答

2

在您的Usertable中,字段名稱電子郵件必須是字符串。 enter image description here

AppDelegate *coreAppDelegate=[[UIApplication sharedApplication]delegate]; 
     NSManagedObjectContext *context=[coreAppDelegate managedObjectContext]; 
     NSEntityDescription *entityDecs=[NSEntityDescription entityForName:@"UserDetail" inManagedObjectContext:context]; 
     NSFetchRequest *request =[[NSFetchRequest alloc]init]; 
     [request setEntity:entityDecs]; 
     // NSLog(@"username = %@",mutarr); 
     NSPredicate *pred=[NSPredicate predicateWithFormat:@"(email=%@)",TxtEmail]; 
     [request setPredicate:pred]; 
     NSManagedObject *matches =nil; 
     NSError *error; 
0

您不能使用NSString方法stringWithFormat建立格式字符串中謂詞使用。儘管名稱相似,predicateWithFormatstringWithFormat的工作方式不同。您應該直接傳遞正確的格式字符串predicateWithFormat

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"email CONTAINS[c] %@", email]; 

(在這種情況下,關鍵的區別是,predicateWithFormat封閉在單引號%@代表的字符串,而stringWithFormat沒有。)