2011-11-09 22 views
2

工作,我有下面的类iOS和coredata:NSPredicate不会在提取请求

#import <Foundation/Foundation.h> 
#import <CoreData/CoreData.h> 


@interface Bankdaten : NSManagedObject 

@property (nonatomic, retain) NSString * blz; 
@property (nonatomic, retain) NSString * name; 
@property (nonatomic, retain) NSString * https; 

@end 

#import "Bankdaten.h" 

@implementation Bankdaten 

@dynamic blz; 
@dynamic name; 
@dynamic https; 

@end 

我检查到该对象的数据是正确的核心数据保存实施在我的sqlite数据库的相应表中。

现在,我想通过这样的要求来获取特定对象:

-(Bankdaten*) ladeBankdaten:(NSString*) blz 
{ 
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Bankdaten" inManagedObjectContext:moc]; 
    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    [request setEntity:entityDescription]; 

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(blz == '%@')", blz]; 
    [request setPredicate:predicate]; 

    NSError *error = nil; 
    NSArray *array = [moc executeFetchRequest:request error:&error]; 
    if (array == nil) { 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    } 

    if([array count] == 0) 
     return nil; 
    return [array objectAtIndex:0]; 
} 

的问题:在这种情况下该数组包含始终为全零零虽然BLZ参数必须匹配的对象,因此我的方法返回数据库中的某些值。所以取回请求应该是正面的。如果我评论

[request setPredicate:predicate]; 

这种没有谓语设置此请求的数据加载细行,因此我认为谓词是某种使用错了我。我在这里做错了什么?

回答

7

你的想法是正确的。更改此:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(blz == '%@')", blz]; 

到:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"blz == %@", blz]; 

随着NSPredicate你不使用围绕字符串参数单引号; predicateWithFormat自动处理它。

括号是好的,但除非你在做需要它们的谓词逻辑,否则最好把它们留出来,并尽可能保持谓词简单。

+0

谢谢你,保存*我的头发被撕掉了一些 - 我也发现这个例如以前工作过:@“guid =='%@'”,itemId –