开发者

Problem with UISearchBar and NSMutableArray

开发者 https://www.devze.com 2023-03-27 16:13 出处:网络
I have a tableview with a search bar. I want to search my whole core data database. I loaded everything into an array here:

I have a tableview with a search bar. I want to search my whole core data database.

I loaded everything into an array here:

NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Entity" inManagedObjectContext:managedObjectContext]; 
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease]; 
[request setEntity:entityDescription]; 
NSError *error = nil; 
NSArray *array = [managedObjectContext executeFetchRequest:request error:&error];   
self.myArray = array;

I'm then searching using this method but getting an error:

- (void) searchTableView {

    NSString *searchText = searchBar.text;
    NSMutableArray *searchArray = [[NSMutableArray alloc] init];

    for (NSDictionary *dictionary in myArray)
    {
        NSString *value =开发者_StackOverflow中文版 [dictionary objectForKey:@"name"];
        [searchArray addObject:value];
    }

    for (NSString *sTemp in searchArray)
    {
        NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if (titleResultsRange.length > 0)
            [copyListOfItems addObject:sTemp];
    }
    [searchArray release];
    searchArray = nil;
}

But when I search, I get the crash here:

MedicalCode *code = [self.copyListOfItems objectAtIndex:indexPath.row]; 
cell.codeLabel.text = code.description;

Error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ICD9Disease objectForKey:]: unrecognized selector sent to instance 0x1be6e0'


You are getting the error because you are asking for an object, but you're trying to set a string, in the line:

NSString *value = [dictionary objectForKey:@"name"];

Since you're getting a string, not an object, what you need to use is valueForKey:

    NSString *value = [dictionary valueForKey:@"name"];


It looks like you're expecting the fetch request to return dictionaries, but it's actually returning instances of the class ICD9Disease.

0

精彩评论

暂无评论...
验证码 换一张
取 消