NSPredicate NSArray和NSObject之间的值匹配

时间:2015-08-30 20:38:43

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

我有一个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.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{

        }

Screenshot

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

  1. 我点击的人数。
  2. 与数字匹配的三个PFUser
  3. 记录:

    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。当我点击&#34; Anna Haro&#34;时,发现person.number 5555228243 ,并在PFUser&#34; 5555228243&#34;中找到,记录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
    

1 个答案:

答案 0 :(得分:1)

我希望我能正确理解

field(2)

and result

此代码优于旧版本。

Dim field As New List(Of String)

Private Sub readcrnFile()
    For Each line In File.ReadAllLines(_filename)
        For i = 1 To 3
            If line.Contains("value" & i) Then
                field.Add(line.Substring(line.IndexOf(":") + 2))
            End If
        Next
    Next
End Sub