Objective-c:将谓词与对象

时间:2016-08-04 13:29:36

标签: objective-c nspredicate

我的表视图有一个搜索栏,直到现在我使用谓词来检查数据数组是否包含搜索栏值。但是现在我的数据数组中有对象,现在我不知道如何使用谓词。这是我的代码:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [cd] %@", self.controller.searchBar.text];

NSMutableArray *temp = [[NSMutableArray alloc] init];

for (int i = 0; i < [self.data count]; i++) {
    Student *student = [[Student alloc] initWithIdentifier:[[self.data objectAtIndex:self.tableView.indexPathForSelectedRow.row] objectForKey:@"id"] name:[[self.data objectAtIndex:self.tableView.indexPathForSelectedRow.row] objectForKey:@"name"] is_public:[[self.data objectAtIndex:self.tableView.indexPathForSelectedRow.row] objectForKey:@"is_public"] password:[[self.data objectAtIndex:self.tableView.indexPathForSelectedRow.row] objectForKey:@"passwort"]];
    [temp addObject:student];
}

self.results = [temp filteredArrayUsingPredicate:predicate];

现在它将搜索栏值与对象进行比较。但它应该将搜索栏值与object.name进行比较。我怎么能这样做?

编辑:

学生代码:

@implementation Student

- (id)initWithIdentifier:(NSString *)identifier name:(NSString *)name is_public:(NSString *)is_public password:(NSString *)password {
    self= [super init];

    if( self ) {
        self.identifier = identifier;
        self.name = name;
        self.is_public = is_public;
        self.password = password;
    }

    return self;
}

- (NSDictionary*)writableRepresentation {
    NSMutableDictionary *writableRepresentation= [NSMutableDictionary dictionaryWithCapacity:4];

    [writableRepresentation setValue:self.identifier forKey:@"Identifier"];
    [writableRepresentation setValue:self.name forKey:@"Name"];
    [writableRepresentation setValue:self.is_public forKey:@"is_public"];
    [writableRepresentation setValue:self.password forKey:@"password"];

    return writableRepresentation;
}

+ (Student*)studentFromDictionary:(NSDictionary*)dictionaryRepresentation {
    return [[Tipprunde alloc] initWithIdentifier:[dictionaryRepresentation valueForKey:@"Identifier"] name:[dictionaryRepresentation valueForKey:@"Name"] is_public:[dictionaryRepresentation valueForKey:@"is_public"] password:[dictionaryRepresentation valueForKey:@"password"]];
}
@end

它似乎不适用于SELF.name。我收到以下错误:

  

- [Student objectForKey:]:无法识别的选择器发送到实例...

0 个答案:

没有答案