检查NSMutableArray是否包含String

时间:2017-01-08 22:45:19

标签: ios objective-c nsmutablearray

我使用以下代码检查用户的姓氏是否存在于我的NSMutableArray(self.friendData)中。也就是说,即使用户姓氏存在于字段"姓氏"中的self.friendData内,它仍然返回该语句的else部分(说明它没有存在于数组中)。我怎样才能解决这个问题?

ViewController.m

 NSMutableDictionary *viewParams2 = [NSMutableDictionary new];
    [viewParams2 setValue:@"friends" forKey:@"view_name"];
    [DIOSView viewGet:viewParams2 success:^(AFHTTPRequestOperation *operation, id responseObject2) {

        self.friendData = (NSMutableArray *)responseObject2;


        NSString *myFriend = [self.neighbourDetail objectForKey:@"last name"];

        if ([self.friendData containsObject:myFriend]) {

            NSLog(@"It contains %@!", myFriend);
            self.addFriend.hidden = YES;

        } else {
            NSLog(@"It does not contain %@", myFriend);
            self.addFriend.hidden = NO;
        }


        [operation responseString];



    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Failure: %@", [error localizedDescription]);
    }];

self.friendData内容:

 "field_friendbio" =         {
            und =             (
                                {
                    format = "<null>";
                    "safe_value" = "There is currently no bio available for this user.";
                    value = "There is currently no bio available for this user.";
                }
            );
        };
        "field_lastname" =         {
            und =             (
                                {
                    format = "<null>";
                    "safe_value" = Niyo;
                    value = Niyo;
                }
            );
        };

1 个答案:

答案 0 :(得分:0)

除非friendData是一个字符串数组,否则我认为你的真正含义是:

if ([self.friendData containsObject:self.neighbourDetail])

编辑:

NSMutableArray *searchResults = [[NSMutableArray alloc] init];

[self.friendData enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

    NSDictionary *friend = obj;

    if ([friend[@"field_lastname"][@"und"][@"value "] isEqualToString:myFriend])
        [searchResults addObject:friend];
}];

NSLog(@"Search Results:%@", searchResults);