【问题标题】:NSPredicate Filter Through Array Of ObjectsNSPredicate 通过对象数组过滤
【发布时间】:2014-09-23 18:42:56
【问题描述】:

这是我的自定义 NSObject 类:

@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];

在 tableViewcellForRowAtIndexPath 方法中并使用它来代替。但我得到一个不同的错误——原因:

'[<personObject 0x109524750> valueForUndefinedKey:]: this class is not key value coding-compliant for the key Paul Smith.'

当我开始在搜索栏中输入内容时,这两个错误都会发生。

这里是row方法...如何在搜索数组中将单元格文本设置为currentPerson.personName

- (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;
}

【问题讨论】:

    标签: ios xcode nspredicate predicate


    【解决方案1】:

    您需要检查personName 密钥而不是_currentPerson.personName 作为密钥。当您提供_currentPerson.personName 时,它将作为密钥并将其视为应该是NSCodingcompliant 的整个密钥。但您没有需要它,您只能将@"personName" 作为字符串传递,它将通过keyValueCoding 工作。用下面的函数替换上面的函数

    - (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:]: unrecognized selector sent to instance 0x1097316a0'
    • 。我检查过它执行得很好。你也不需要 BOOL *只需使用 BOOL。- (id) initWithName: (NSString *) personName Photo: (UIImage *) photo Invited: (BOOL) invited;
    • 可能你正在做更多的事情。它在哪一行崩溃
    • 哦...问题出在显示搜索内容上...这样可行,但是如何将标签单元格文本设置为数组中 PersonObject 中的 personName。
    猜你喜欢
    • 1970-01-01
    • 2022-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-20
    • 2019-05-05
    • 1970-01-01
    • 2019-12-02
    相关资源
    最近更新 更多