【问题标题】:NSPredicate Value matching between NSArray and NSObjectNSArray 和 NSObject 之间的 NSPredicate 值匹配
【发布时间】:2015-08-30 20:38:42
【问题描述】:

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

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

我想比较这两个数组之间的电话号码。并记录与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”,记录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

【问题讨论】:

    标签: ios objective-c parse-platform nsarray nspredicate


    【解决方案1】:

    希望我理解正确

      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(@"");
    

    这段代码比老v好。

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

    【讨论】:

    • 很好,谢谢!但这只给了我一个 PFUser(最后一个匹配的),我想在结果 NSArray 中添加每个 PFUser。
    • 编辑了我的答案。 np.
    • 不错的编辑! :) 我们很接近我的预期。我用 cellForRow、图像和日志编辑了我的帖子。非常感谢...
    • 不,我只想记录我点击的 pfuser 行……你知道怎么做吗?
    • 我改变了:NSArray *result = [self.allUsers filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"%K = %@", @"phone", obj.number]];我根据我对 NSLog 的期望编辑了我的帖子
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-24
    • 1970-01-01
    相关资源
    最近更新 更多