核心数据搜索错误(使用NSString)

时间:2014-04-16 05:56:02

标签: core-data ios7

我有用于核心数据的搜索问题!

-CASE 1 //它不起作用

-(BOOL)isHeThere:(NSString*)tmpIdm{

    NSString *idm = [tmpIdm uppercaseString];
    NSLog(@"%@",idm);

    AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
    NSFetchRequest *request = [[NSFetchRequest alloc] init];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"HRData"
                                              inManagedObjectContext:appDelegate.managedObjectContext];
    [request setEntity:entity];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"userIdm" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [request setSortDescriptors:sortDescriptors];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userIdm=%@",idm];
    [request setPredicate:predicate];

    NSError *error = nil;
    NSArray *fetchResults = [appDelegate.managedObjectContext executeFetchRequest:request
                                                                     error:&error];

    NSLog(@"%@",fetchResults);
 // cut follow code
}

LOG

2014-04-16 15:07:25.716 MyProject[693:60b] 011204121413310F
2014-04-16 15:07:25.734 MyProject[693:60b] (
)

结果一无所获。

-Case2 //它有效

-(BOOL)isHeThere:(NSString*)tmpIdm{

    NSString *idm = [tmpIdm uppercaseString];
    NSLog(@"%@",idm);

    AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
    NSFetchRequest *request = [[NSFetchRequest alloc] init];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"HRData"
                                              inManagedObjectContext:appDelegate.managedObjectContext];
    [request setEntity:entity];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"userIdm" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [request setSortDescriptors:sortDescriptors];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userIdm=%@",@"011204121413310F"];
    [request setPredicate:predicate];

    NSError *error = nil;
    NSArray *fetchResults = [appDelegate.managedObjectContext executeFetchRequest:request
                                                                     error:&error];

    NSLog(@"%@",fetchResults);

// cut follow code
]

日志

2014-04-16 15:05:45.198 MyProject[681:60b] 011204121413310F
2014-04-16 15:05:45.220 MyProject[681:60b] (
    "<HRData: 0x16534d30> (entity: HRData; id: 0x165381e0 <x-coredata://9319373D-1532-42E0-A366-57E5B6E3EBD0/HRData/p47914> ; data: <fault>)"
)

两个案例使用相同的代码“1234F”

为什么我得到不同的结果?

问题是什么?

+我编辑了代码(并编译日志)

1 个答案:

答案 0 :(得分:1)

像这样解决

NSPredicate *pred=[NSPredicate predicateWithFormat:@"userIdm=%@",idm];
NSLog(@"%@",pred);

日志

2014-04-16 15:23:19.832 SecurityGate[752:60b] userIdm == "011204121413310F
"

\ n&lt; - 问题