NSPredicate过滤通过的数组对象
问题描述:
。这是我的自定义NSObject类:NSPredicate过滤通过的数组对象
@interface personObject : NSObject
@property (nonatomic, copy) NSString *personName;
@property (nonatomic, copy) UIImage *personPhoto;
@property BOOL invited;
- (id) initWithName: (NSString *) personName Photo: (UIImage *) photo Invited: (BOOL *) invited;
@end
@implementation personObject
@synthesize personName = _personName;
@synthesize personPhoto = _personPhoto;
@synthesize invited = _invited;
- (id) initWithName:(NSString *)personName Photo:(UIImage *)photo Invited:(BOOL *)invited{
self = [super init];
if (self) {
self.personName = personName;
self.personPhoto = photo;
self.invited = invited;
}
return self;
}
@end
我初始化三人在viewDidLoad中的对象。 现在我试图
[NSPredicate predicateWithFormat:@"personName beginswith[c], searchText];
但我得到一个错误 -
-[personObject copyWithZone:]: unrecognized selector sent to instance 0x109684330'
所以,我想这:
- (void) filterContententForSearchText: (NSString *) searchText scope:(NSString *) scope{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K beginsWith[c] %@",_currentPerson.personName, searchText ];
self.searchArray = [self.peopleArray filteredArrayUsingPredicate:predicate];
}
所以我试图设置
_currentPerson = [self.peopleArray objectAtIndex.row];
在
tableViewcellForRowAt IndexPath方法,并用它来代替。但我得到一个不同的错误 - 原因:
'[<personObject 0x109524750> valueForUndefinedKey:]: this class is not key value coding-compliant for the key Paul Smith.'
这两个错误发生在我开始键入搜索栏时。
继承人的排法......如何设置单元格文本搜索阵列
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
personObject *currentPerson = [self.peopleArray objectAtIndex:indexPath.row];
self.personCell = (personCell *) [self.tableView dequeueReusableCellWithIdentifier:@"personCell"];
if (tableView == self.searchDisplayController.searchResultsTableView) {
self.personCell.nameLabel.text = [self.searchArray objectAtIndex:indexPath.row];
}
else{
self.personCell.nameLabel.text = currentPerson.personName;
}
return _personCell;
}
答
您需要检查personName
关键不_currentPerson.personName
作为key.When你给_currentPerson.personName
中currentPerson.personName它采取它的关键,并认为它是应该是NSCoding
compliant.But你不需要它整个键你可以通过只@"personName"
为字符串,它会通过keyValueCoding
.Replace工作上面的函数下面
- (void) filterContententForSearchText: (NSString *) searchText scope:(NSString *) scope{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K beginsWith[c] %@",@"personName", searchText ];
self.searchArray = [self.peopleArray filteredArrayUsingPredicate:predicate];
NSLog(@"%@",seachArray);
}
仍然得到一个错误----原因:' - [personObject copyWithZone:]:无法识别的选择器发送到实例0x1097316a0' – Peter 2014-09-23 19:21:38
。它正在执行罚款我check.You也不需要BOOL *只是使用BOOL.'-( id)initWithName:(NSString *)personName照片:(UIImage *)photo Invited:(BOOL)invite;' – codester 2014-09-23 19:26:04
可能是你正在做更多的事情。在哪一行崩溃 – codester 2014-09-23 19:27:14