0

quick question regarding Array's in xcode. I have ht efollowing code, which is supposed to go through an array of strings which it has got through php and JSON, and trun these strings into a custom object with the strings as the ivars for the object then add that object to a new array:

for (int i = 0; i<[list count]; i++) {
        Article *article = [[Article alloc] init]; //creates custom object
        article.uid = [[list objectAtIndex:i] objectAtIndex:0];
        article.title = [[list objectAtIndex:i] objectAtIndex:1]; //adds string as ivars
        article.description = [[list objectAtIndex:i] objectAtIndex:2];
        articleArray = [[NSMutableArray alloc] init]; //inits the new array
        [articleArray addObject:article]; //should add the object but seems to fail
        [article release]; //releases the object
        NSLog(@"%@", article.description);
    }
    NSLog(@"%d", [articleArray count]);
    NSLog([articleArray description]);
}

The code does return the correct values using NSLog(@"%@", article.description); but not the correct length for the new array and it only adds one value to the array which is the string for article.description which makes no sense to me. The list array contains 2 elements each of which are arrays in themselves containing the strings.

1 Answer 1

6

You're recreating the articleArray in every loop. Declarate it outside, and it will work:

NSMutableArray *articleArray = [[NSMutableArray alloc] init]; //inits the new array
for (int i = 0; i<[list count]; i++) {
        Article *article = [[Article alloc] init]; //creates custom object
        article.uid = [[list objectAtIndex:i] objectAtIndex:0];
        article.title = [[list objectAtIndex:i] objectAtIndex:1]; //adds string as ivars
        article.description = [[list objectAtIndex:i] objectAtIndex:2];
        [articleArray addObject:article]; //should add the object but seems to fail
        [article release]; //releases the object
        NSLog(@"%@", article.description);
    }
    NSLog(@"%d", [articleArray count]);
    NSLog([articleArray description]);
}

You also may want to use the nicer for(NSArray *listElement in list) syntax instead.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! I knew it would be something simple I was missing.
Note for more recent readers who stumble across this like I did: the code sample includes an example of releasing a variable after use - this explicit release is forbidden in a project using Automatic Reference Counting (ARC) - just ignore this line and it will work fine.
@user3152873 - To be fair, that's the case with everything here written using manual reference counting, of which there are around 2000 answers in a quick search.

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.