NSPredicate NSArray 和 NSObject 之间的值匹配

NSPredicate Value matching between NSArray and NSObject

我有一个 NSArray (self.allUsers),里面有 PFUser 个对象 :

"<PFUser: 0x7ff578f501f0, objectId: qvFwzbBGky, localId: (null)> {\n    friends = \"<PFRelation: 0x7ff578f4eef0, 0x0.(null) -> _User>\";\n    phone = 0;\n    surname = test;\n    username = \"test@gmail.com\";\n}"

我可以使用 [self.allUsers valueForKey:@"phone"]

访问 phone 号码

我还有另一个 NSArray (self.sectionedPersonName),里面有 NSObject Person(按部分排序)。我可以使用 person.number.

访问 phone 号码

我想比较这两个数组之间的 phone 数字。并在匹配 PFUser objectId ([self.allUsers valueForKey:@"objectId"];)

时记录

也许我必须使用 NSPredicate CONTAINS ? 我不知道 NSObject 和 PFUser 是否可行。

之后,我想更改匹配的单元格的颜色。

在 didSelect 中,当我点击它时,只需从 PFUser 中记录 objectId。我们要把PFUser和NSObject做一个对应...

我成功显示了匹配的 PFUser 的 objectId:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        cell.accessoryType = UITableViewCellAccessoryNone;
        Person *person = [[self.sectionedPersonName objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

        cell.textLabel.text = person.fullName;
        cell.detailTextLabel.text = person.number;

        if ( [[self.allUsers valueForKey:@"phone"] containsObject:person.number] ) {
            // do found
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        } else {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Person *person = [[self.sectionedPersonName objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        __block PFUser *foundedPFUser;
        NSMutableArray *realResult   = [NSMutableArray new];
        [self.tableData enumerateObjectsUsingBlock:^(Person *obj, NSUInteger idx, BOOL *stop) {
        NSArray *result = [self.allUsers filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"%K = %@", @"phone", obj.number]];
            if(result.count){
                [realResult addObjectsFromArray:result];
            }
        }];

        NSLog(@"%@",realResult);

        if (cell.accessoryType == UITableViewCellAccessoryCheckmark){
            NSLog(@"%@", person.number);
        }
        else{

        }

现在,当我点击一个单元格时,该日志:

  1. 我点击的人数
  2. 匹配号码的三个PFUser

日志:

2015-08-31 12:02:59.727 ChillN[691:18988] (
    "<PFUser: 0x7f992a4df7e0, objectId: bEO3D62Cvo, localId: (null)> {\n    friends = \"<PFRelation: 0x7f992a4a3b60, 0x0.(null) -> _User>\";\n    phone = 5555228243;\n    surname = test4;\n    username = \"test4@gmail.com\";\n}",
    "<PFUser: 0x7f992a4a3d30, objectId: rlG21mKFJZ, localId: (null)> {\n    friends = \"<PFRelation: 0x7f992a4e08d0, 0x0.(null) -> _User>\";\n    phone = 31560987;\n    surname = test2;\n    username = \"test2@gmail.com\";\n}",
    "<PFUser: 0x7f992a47af90, objectId: 7X0OVanRZ9, localId: (null)> {\n    friends = \"<PFRelation: 0x7f992a4e0970, 0x0.(null) -> _User>\";\n    phone = 7075551854;\n    surname = test3;\n    username = \"test3@gmail.com\";\n}"
)
2015-08-31 12:02:59.727 ChillN[691:18988] 5555228243

现在我只希望匹配好的 PFUser。当我点击"Anna Haro"时,发现person.number5555228243,在PFUser中找到“5555228243”,并记录[=的objectId 16=].

我期待什么:

日志:

2015-08-31 12:02:59.727 ChillN[691:18988] (
    "<PFUser: 0x7f992a4df7e0, objectId: bEO3D62Cvo, localId: (null)> {\n    friends = \"<PFRelation: 0x7f992a4a3b60, 0x0.(null) -> _User>\";\n    phone = 5555228243;\n    surname = test4;\n    username = \"test4@gmail.com\";\n}"
)
2015-08-31 12:02:59.727 ChillN[691:18988] 5555228243

希望我理解正确

  PFUser *pfUser1 = [PFUser new];
  pfUser1.objectId = @"qvFwzbBGky";
  pfUser1.phone    = @"123";

  PFUser *pfUser2 = [PFUser new];
  pfUser2.objectId = @"bar";
  pfUser2.phone    = @"987";

  NSArray *allUsers = @[ pfUser1, pfUser2 ];

  Person *person = [Person new];
  person.phoneNumber = @"123";

  NSArray *anotherArray = @[ person ];


  __block BOOL found;
  __block PFUser *foundedPFUser;
  [anotherArray enumerateObjectsUsingBlock:^(Person *obj, NSUInteger idx, BOOL *stop) {

      NSArray *result = [allUsers filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"%K contains[c] %@", @"phone", obj.phoneNumber]];

      if(result.count){

          found = YES;
          foundedPFUser = result.firstObject;
          *stop = YES;
      }

  }];

  NSLog(@"");

这段代码比旧版好

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  {
      __block PFUser *foundedPFUser;
      NSMutableArray *realResult   = [NSMutableArray new];
      [self.tableData enumerateObjectsUsingBlock:^(Person *obj, NSUInteger idx, BOOL *stop) {
          NSArray *result = [self.allUsers filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"%K contains[c] %@", @"phone", obj.number]];
          if(result.count){
              [realResult addObjectsFromArray:result];
          }
      }];

      NSLog(@"%@",realResult);
  }