繁体   English   中英

NSArray和NSObject之间的NSPredicate值匹配

[英]NSPredicate Value matching between NSArray and NSObject

我有一个NSArrayself.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"]访问电话号码

我有另一个NSArrayself.sectionedPersonName )里面有NSObject Person (按部分排序)。 我可以使用person.number访问电话号码。

我想比较这两个阵列之间的电话号码。 并记录与PFUser objectId( [self.allUsers valueForKey:@"objectId"]; )相匹配的PFUser

也许我必须使用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”,并记录PFUser的objectId。

我期望的是:

日志:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM