(NSManagedObjectContext *)managedObjectContext,NSInvalidArgumentException

时间:2014-04-27 09:09:28

标签: uisearchbar nsmanagedobject

我有一个应用程序,显示保存在核心数据中的文本。在这篇文章中,我添加了SearchBarSearchDisplayController。当我在SearchBar内部提示并输入内容时,我立刻就有了这个

NSInvalidArgumentException: **'NSInvalidArgumentException', reason: '-[NSManagedObject compare:options:range:]: unrecognized selector sent to instance 0x8cb6460'**.

0x8cb6460是名为NSString的{​​{1}}变量。 这是两段代码:

第一块

tempSearchStr

第二块

- (NSManagedObjectContext *)managedObjectContext
{
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }

    return context;
}

- (void)searchInNoteMe { NSString *searchString = self.searchBar.text; for (NSString *tempSearchStr in notemearray) { NSComparisonResult NSCResult = [tempSearchStr compare:searchString options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchString length])]; if (NSCResult == NSOrderedSame) { [notemearrayfiltered addObject:tempSearchStr]; } } } 的代码:

notemearray

1 个答案:

答案 0 :(得分:0)

notemearray包含NSManagedObject s而不是NSString s,这就是异常告诉您的内容。

您想在CoreData对象的compare:options:range:属性上调用noteme

- (void)searchInNoteMe
{
    NSString *searchString = self.searchBar.text;

    for (NSManagedObject *note in notemearray) {
        NSString *noteString = [note valueForKey:@"noteme"];
        NSComparisonResult NSCResult = [noteString compare:searchString 
                                                   options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)
                                                     range:NSMakeRange(0, [searchString length])];
        if (NSCResult == NSOrderedSame) {
            [notemearrayfiltered addObject:note];
        }
    }
}

如果您想更新noteme上的NSManagedObject属性,可以执行以下操作:

[note setValue:@"New string" forKey:@"noteme"];