1

So I have an array of student names. I'm running test data so I'm going through a loop allocating and initializing each student then just throwing it in stuArr - If I NSLog my Student.h init() method it will give me the names I would expect but when I try to call them outside of my method I get null values:

- (void)viewDidLoad
{
    [super viewDidLoad];
    for (int i = 0; i < 5; i++) {
        stuArr = [stuArr arrayByAddingObject:[[Student alloc] init]];
        id test = [stuArr objectAtIndexedSubscript:i];
        NSLog(@"%@", [test stuName]);
    }
}

Am I missing something vital here? If need be I can throw in my Student.m File but everything seems fine there.

4
  • 1
    Perhaps you forgot to alloc+init the array stuArray ? Commented Feb 20, 2013 at 20:29
  • Isn't that what it's doing in the loop? Right now I have NSArray as a property that synthesizes in the .m file. Commented Feb 20, 2013 at 20:30
  • So your init for Student assigns the name? Commented Feb 20, 2013 at 20:30
  • 2
    @Howdy_McGee: No, you have to init the array, e.g. stuArr = [[NSMutableArray alloc] init]; otherwise it is nil. Commented Feb 20, 2013 at 20:32

2 Answers 2

5

I guess that you should alloc + init stuArr before this line:

stuArr = [stuArr arrayByAddingObject:[[Student alloc] init]]; 

Or try to do something like this:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSMutableArray *mutStudentsArray = [NSMutableArray array];

    for (int i = 0; i < 5; i++)
    {
        [mutStudentsArray addObject:[[Student alloc] init]];
        id test = [mutStudentsArray objectAtIndex:i];
        NSLog(@"%@", [test stuName]);
    }

    stuArr = [NSArray arrayWithArray:mutStudentsArray];
}
Sign up to request clarification or add additional context in comments.

4 Comments

@Howdy_McGee, you mean [[NSMutableArray alloc]init], right?
Right now it's just static. I'm just using test data so I didn't see a need to make me mutable.
@CodaFi nope, he mean [[NSArray alloc] init], because he uses stuArr = [stuArr arrayByAddingObject:[[Student alloc] init]] instead of using NSMuttableArray
@tikhop I was hoping to go for an implicit "You should be using a mutable array for this" :)
1

tikhop is right: if you fill an array in a for loop you should initialize NSMutableArray (mutable Array because you are changeing the array in the loop) before you fill it. in the loop you adding objects to the array with [array addObject:id].

What [array arrayByAddingObject:id] does is it creates a copy of the receiving Array and adds the new object to the end. which means to use that you will need to do something like the following (doesn't make much sense to do that in an for-loop though but maybe it helps to understand):

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray *stuArr = [NSArray alloc] init]; //stuArr can be used now

    for (int i = 0; i < 5; i++) {
        //someCopiedArr is some existing array- arrayByAddingObject will copy someArr and add an object
        stuArr = [someArr arrayByAddingObject:[[Student alloc] init]];
        id test = [stuArr objectAtIndexedSubscript:i];
        NSLog(@"%@", [test stuName]);
    }
}

In the end stuArr will be a copy of someArr just with the object added to the end.

Comments

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.