NSObjects IOS 的 NSPredicate 问题

NSPredicate issue with NSObjects IOS

我有一个 NSObject:

@interface ContactAlphaB : NSObject

+ (ContactAlphaB *)contactWithFirstName:(NSString *)firstName lastName:(NSString *)lastName username:(NSString *)username
 linkAvatar:(NSString *)linkAvatar registerType:(NSString *)registerType
email:(NSString *)email mobile:(NSString *)mobile;

@end

我已经保存了这个对象中的所有信息。

@property (nonatomic) NSString *firstName;
@property (nonatomic) NSString *lastName;
@property (nonatomic) NSString *username;
@property (nonatomic) NSString *linkAvatar;
@property (nonatomic) NSString *registerType;
@property (nonatomic) NSString *stremail;
@property (nonatomic) NSString *strmobile;

而且我在 tableView 中有显示

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return self.sectionIndexTitles;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.sectionIndexTitles count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSString *sectionIndexTitle = self.sectionIndexTitles[section];
    return [self.alphabetizedDictionary[sectionIndexTitle] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CellListsFriend *cell = [tableView dequeueReusableCellWithIdentifier:@"cellListsFriend" forIndexPath:indexPath];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    ContactAlphaB *contact = [self objectAtIndexPath:indexPath];
    cell.lblNameFriend.text = contact.fullName;    
    // Rounded Rect for cell image
    cell.imgAvatarFriends.layer.borderWidth=2;
    cell.imgAvatarFriends.layer.borderColor=[[UIColor whiteColor]CGColor];
    [cell.imgAvatarFriends.layer setCornerRadius:cell.imgAvatarFriends.frame.size.width/2];
    [cell.imgAvatarFriends.layer setMasksToBounds:YES];

    return cell;
}

- (void)setContacts:(NSArray *)contacts {
    self.alphabetizedDictionary = [CGLAlphabetizer alphabetizedDictionaryFromObjects:contacts usingKeyPath:@"lastName"];
    self.sectionIndexTitles = [CGLAlphabetizer indexTitlesFromAlphabetizedDictionary:self.alphabetizedDictionary];

    [self.tableView reloadData];
}

- (ContactAlphaB *)objectAtIndexPath:(NSIndexPath *)indexPath {
    NSString *sectionIndexTitle = self.sectionIndexTitles[indexPath.section];
    return self.alphabetizedDictionary[sectionIndexTitle][indexPath.row];
}

通过这段代码,我可以在 tableView 中显示我所有的好友列表。

现在,我想做搜索功能(搜索我的朋友)。

所以,我正在使用 NSPredicate *filter = [NSPredicate predicateWithFormat:@"SELF == %@", searchString]; 来实现这个功能。

但是,我无法在 NSObject 中使用 filteredArrayUsingPredicate 进行搜索。

如何实现搜索功能。

请帮帮我!

*****************编辑:

NSArray *arr = [FriendList MR_findAll];
    if (arr.count > 0) {
        for (int i = 0; i < arr.count; i++) {
            FriendList *friendList = [arr objectAtIndex:i];
            [delegate.arrFriendListsAdded addObject:friendList.username];
            ContactAlphaB *contact = [ContactAlphaB contactWithFirstName:friendList.firstName
                                                                lastName:friendList.lastName
                                                                username:friendList.username                                                            
                                                              linkAvatar:friendList.avatar
                                                            registerType:[NSString stringWithFormat:@"%@", friendList.registerType]
                                                                   email:friendList.email
                                                                  mobile:friendList.phoneNumber
                                      ];

            [_mucontacts addObject:contact];

        }
    }

_mucontacts 是 NSMutableArray。

当我记录这个数组时:

<__NSArrayM 0x4e48050>(
<ContactAlphaB: 0x4f55b90>,
<ContactAlphaB: 0x13e8dc0>,
<ContactAlphaB: 0x4f2bd30>,
<ContactAlphaB: 0x4e6a490>
)

是保存对象。搜索时,我想搜索姓氏。

所以,当我搜索姓氏时,它是空的。

试试这个

  NSString *searchString = @"your last name here";
  NSPredicate *lastname = [NSPredicate predicateWithFormat:@"self.lastname contains[cd] %@", searchString];
  NSArray *filteredArray = [mucontacts filteredArrayUsingPredicate:lastname];