Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Updating IOS Array

How do I update an ios array.

can you clarify what you mean by update? what are you trying to accomplish with the array

So in my previous question, I needed to know how to make a variable available in all the files of my Xcode project. In the ViewController that handles logins, the NSArray 'facultyList' is having the newUser that is being added, added to the array (facultyList). However, when I try to view the contents of the array using NSLog, it prints out (null). At this point I am unsure of how to continue because the real problem is that it is not being added to the array.

- (void)viewDidLoad
{
    [super viewDidLoad];

    MIAppDelegate *myAppDelegate = [[UIApplication sharedApplication] delegate];
    NSMutableArray *facultyList = myAppDelegate.facultyListSource;

}

- (IBAction)signup:(id)sender {
    NSString *username=[self.usernameField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *password=[self.passwordField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *email=[self.emailField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];



    if ([username length]==0 || [password length]==0||[email length]==0||[self.accessCode.text intValue]!=32){
        UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Oops!" message:@"Make sure you enter a username, password and email address!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

        [alertView show];
    }
    else {
        PFUser *newUser=[PFUser user];
        newUser.username=username;
        newUser.password=password;
        newUser.email=email;
        if ([self.facultyField.text intValue]!=73) {
            NSLog(@"This is the problem");
        }
        else{

            [facultyList addObject:newUser];

            NSLog(@"%@, Test",facultyList);

        }
        [newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (error) {
                UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Sorry" message:[error.userInfo objectForKey:@"error"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alertView show];
            }
            else {
                [self.navigationController popToRootViewControllerAnimated:YES];
            }
        }];



    }

};
@end

That is my code. facultyList is declared as an array in the header file. Again, thank you so much.

6 Answers

As far as I know you need a Mutable Array in order to be updated dynamically.

Look at NSMutableArray section.

It is a NSMutbale Array. I just need to know how to update the view

here is your problem:

- (void)viewDidLoad
{
    [super viewDidLoad];

    MIAppDelegate *myAppDelegate = [[UIApplication sharedApplication] delegate];
    NSMutableArray *facultyList = myAppDelegate.facultyListSource;

}

you are assigning the appDelegate array property to a local array variable which is only available in the scope of the viewDidLoad method. you need to add a property to your view controller to store it. try adding something like this to your viewcontroller's .h

@property (nonatomic) NSMutableArray *facultyList;

and then using

- (void)viewDidLoad
{
    [super viewDidLoad];

    MIAppDelegate *myAppDelegate = [[UIApplication sharedApplication] delegate];
    self.facultyList = myAppDelegate.facultyListSource;

}

- (IBAction)signup:(id)sender {
    NSString *username=[self.usernameField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *password=[self.passwordField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *email=[self.emailField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];



    if ([username length]==0 || [password length]==0||[email length]==0||[self.accessCode.text intValue]!=32){
        UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Oops!" message:@"Make sure you enter a username, password and email address!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

        [alertView show];
    }
    else {
        PFUser *newUser=[PFUser user];
        newUser.username=username;
        newUser.password=password;
        newUser.email=email;
        if ([self.facultyField.text intValue]!=73) {
            NSLog(@"This is the problem");
        }
        else{

            [self.facultyList addObject:newUser];

            NSLog(@"%@, Test", self.facultyList);

        }
        [newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (error) {
                UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Sorry" message:[error.userInfo objectForKey:@"error"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alertView show];
            }
            else {
                [self.navigationController popToRootViewControllerAnimated:YES];
            }
        }];



    }

};

Alright, I did that. Unfortunately the console logged '(null), Test' Any Ideas?

yep. log the value of the app delegate array property in viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];

    MIAppDelegate *myAppDelegate = [[UIApplication sharedApplication] delegate];

    NSLog(app delegate faculty list: %@", myAppDelegate.facultyListSource);
    self.facultyList = myAppDelegate.facultyListSource;

}

if that logs null then youve got an issue whereever you set the value of that appDelegate property

Ok, it logs null. So as you said I should look where I set the value of the appDelegate property. When you say that, what do you mean? Thank you again, you are extremely helpful.

In my appDelegate.h

#import <UIKit/UIKit.h>



@interface MIAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) NSMutableArray *facultyListSource;



@end

actually. im wrong. its fine if its null there, i forgot your array was mutable so its probably empty at this point anyways. your issue might be that you are adding that newUser object to the array before saving it/calling the signUp method. try saving it first by calling the sign up method, then adding it to the array and logging it then in the completion block:

 else {
        PFUser *newUser=[PFUser user];
        newUser.username=username;
        newUser.password=password;
        newUser.email=email;

        [newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (error) {
                UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Sorry" message:[error.userInfo objectForKey:@"error"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alertView show];
            }
            else {

                  if ([self.facultyField.text intValue]!=73) {
            NSLog(@"This is the problem");
                  }
        else{

            [self.facultyList addObject:newUser];

                   NSLog(@"%@, Test", self.facultyList);

        }


                [self.navigationController popToRootViewControllerAnimated:YES];
            }
        }];



    }

So everything seems to be running fine, but it still logs out null. Is this the standard behavior, and if so, how would I know if the function is properly working.

you definitely should have an object in that array at that point(unless that object is null, which it shouldnt be) so no that is not working correctly.

First, here is my code

#import "SignupViewController.h"


@interface SignupViewController ()

@end

@implementation SignupViewController



- (void)viewDidLoad
{
    [super viewDidLoad];

    MIAppDelegate *myAppDelegate = [[UIApplication sharedApplication] delegate];
    NSLog(@"app delegate faculty list: %@", myAppDelegate.facultyListSource);
    self.facultyList = myAppDelegate.facultyListSource;

}

- (IBAction)signup:(id)sender {
    NSString *username=[self.usernameField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *password=[self.passwordField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *email=[self.emailField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];



    if ([username length]==0 || [password length]==0||[email length]==0||[self.accessCode.text intValue]!=32){
        UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Oops!" message:@"Make sure you enter a username, password and email address!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

        [alertView show];
    }
    else {
        PFUser *newUser=[PFUser user];
        newUser.username=username;
        newUser.password=password;
        newUser.email=email;
        if ([self.facultyField.text intValue]!=73) {
            NSLog(@"This is the problem");
        }
        else{


        }
        [newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (error) {
                UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Sorry" message:[error.userInfo objectForKey:@"error"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alertView show];
            }
            else {

                [self.facultyList addObject:newUser];

                NSLog(@"%@, Test", self.facultyList);

                [self.navigationController popToRootViewControllerAnimated:YES];
            }
        }];



    }
}
@end

I think the problem is that newUser is part of the Parse framework, and after the 'newUser' is signed up, it is no longer accessible. What do you think of this theory.

that could be correct. after the user is signed up you should be able to access it using [PFUser currentUser] so perhaps try

[newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (error) {
                UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Sorry" message:[error.userInfo objectForKey:@"error"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alertView show];
            }
            else {

                [self.facultyList addObject: [PFUser currentUser]];

                NSLog(@"%@, Test", self.facultyList);

                [self.navigationController popToRootViewControllerAnimated:YES];
            }
        }];

maybe that will work

ughh.This stuff is so frustrating sometimes. This is the console

app delegate faculty list: (null)
(null), Test

Do you know how we can access the newly signed up user any other way? Again, I want to thank you for your generosity, kindness, and patience with me?

It might be returning null still because the code we are adding is still within the signup in background function.