我有一个应用程序,显示保存在核心数据中的文本。在这篇文章中,我添加了SearchBar
和SearchDisplayController
。当我在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
答案 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"];