Trouble accessing Mutable array

Posted by Jared Gross on Stack Overflow See other posts from Stack Overflow or by Jared Gross
Published on 2013-10-27T02:43:52Z Indexed on 2013/10/27 3:54 UTC
Read the original article Hit count: 128

Filed under:
|
|
|
|

Im having trouble with my for loop where I am trying to index user names. I am able to separate my original array into individual objects but am not able to send the value to a new array that I need to reference later on. The value and count for userNames in my self.userNamesArray = userNames; line is correct. But right after that when I log self.userNamesArray, I get (null). Any tips cause I'm not completely sure I'm cheers!

.h

@property (nonatomic, copy) NSMutableArray *userNamesArray;

.m

- (void)viewWillAppear:(BOOL)animated {

self.friendsRelation = [[PFUser currentUser] objectForKey:@"friendsRelation"];

PFQuery *query = [self.friendsRelation query];
[query orderByAscending:@"username"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (error) {
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
    else {
        self.friends = objects;

        NSArray *users = [self.friends valueForKey:@"username"];
        NSLog(@"username:%@", users);
        //Create an array of name wrappers and pass to the root view controller.
        NSMutableArray *userNames = [[NSMutableArray alloc] initWithCapacity:[self.friends count]];

        for (NSString *user in users) {

        componentsSeparatedByCharactersInSet:charSet];
            NSArray *nameComponents = [user componentsSeparatedByString:@" "];

            UserNameWrapper *userNameWrapper = [[UserNameWrapper alloc]    initWithUserName:nil nameComponents:nameComponents];

            [userNames addObject:userNameWrapper];
        }

        self.userNamesArray = userNames;

        NSLog(@"userNamesArray:%@",self.userNamesArray);

        [self.tableView reloadData];
    }

Here's the code where I need to reference the self.userNamesArray where again, it is comping up nil.

- (void)setUserNamesArray:(NSMutableArray *)newDataArray {

if (newDataArray != self.userNamesArray) {
    self.userNamesArray = [newDataArray mutableCopy];
    if (self.userNamesArray == nil) {
        self.sectionsArray = nil;
        NSLog(@"user names empty");
    }
    else {
        [self configureSections];
    }
}

}

© Stack Overflow or respective owner

Related posts about ios

Related posts about objective-c