0

I'm trying to check if NSString 'testing' (47) exists inside of my NSMutableArray 'self.checkfriendData'. I'm using the code below, though after logging my if statement it appears as though it's never executed (even though the statement is true - see console data below, uid = 47, and thus hiding my object should fire?) Any idea as to why this isn't working? Help is much appreciated!

ViewController.m

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

        self.checkfriendData = (NSMutableArray *)responseObject;

        NSString *testing = @"47";


        NSArray *friendorNo = self.checkfriendData;

        if ([friendorNo containsObject:testing]) // YES
        {

            self.addFriend.hidden = YES;
        }


    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    }];

Here's what's inside self.checkfriendData:

 2017-05-18 19:36:07.266529-0700 This is the friend data check (
            {
            body = "My name is Britt";
            friendphoto = "/sites/default/files/stored/x.jpg";
            "node_title" = "Britt";
            uid = 47;
        }
    )
2
  • What is the type of the object associated with key uid? Also should friendorNo be of type NSDictionary? Commented May 19, 2017 at 3:02
  • @CRD uid would be NSDictionary (it would appear) - that said, if I simply change NSString testing to NSDictionary *testing, would this solve my issue? Commented May 19, 2017 at 3:07

1 Answer 1

1

It appears that your NSArray contains NSDictionarys and you are asking if the array contains an NSString. The answer will always be no as the array doesn't directly contain any NSStrings.

If you want to search for the uid of 47 you will have to iterate over the array and check the uid key of each NSDictionary for the value 47.

The code for this would look something like:

for (NSDictionary *dict in friendorNo) {
    if ([dict[@"uid"] isEqualToString:testing]) {
        self.addFriend.hidden = YES;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Any idea how this code would look? I've tried checking the specific dictionary uid for 47 before, and it still doesn't seem to execute? E.g. if self.checkfriendData objectForKey:@"uid" contains 47...
Lol made some minor edits, all good. That said, should I define uid as an NSDictionary in my .h file then? It's not currently defined (I thought objectForKey states the dictionary to look for inside the data being returned?)
uid is an NSString inside an NSDictionary that is inside an NSArray. You can't simply ask the NSArray if it contains an NSString with value 47 because the NSArray knows nothing about the structure of the NSDictionarys that it contains.
Thanks a million! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.