简体   繁体   中英

Checking if NSMutableArray contains String

I'm using the below code to check if a user's last name exists inside of my NSMutableArray (self.friendData). That said, even though the user last name exists inside of the self.friendData in the field "last name", it's still returning the else portion of the statement (stating it doesn't exist in the array). How can I fix this?

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 contents:

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

Unless friendData is an array of strings, I think what you really meant is this:

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

EDIT:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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