0

Currently I'm populating an array in one line, e.g.

self.monthMonths = [[NSArray alloc] initWithObjects:@"January", @"February", @"March", @"April", @"May", @"June",@"July",@"August",@"September",@"October",@"November",@"December", nil]; 

What is the syntax to add these elements one at a time as I want to pull the data from a database. I'm using the months of the year as an example.

while([results next]) {
    NSString *months = [results stringForColumn:@"month"];
    self.month = [[NSArray alloc] initWithObjects:@"month",nil];
    //[NSArray 
    NSLog(@"Month: %@",month);
}
1

2 Answers 2

2

Create an NSMutableArray and add the objects to it one by one with addObject

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

Comments

1

You need to use NSMutableArray instead, and call -addObject:

2 Comments

Cheers - message understood. Out of interest what is the difference in a mutable array of the two setup types: NSMutableArray *monthly = [NSMutableArray arrayWithCapacity:12]; or NSMutableArray *monthly = [[NSMutableArray alloc] init];
If I remember correctly, -initWithCapacity: makes room for the declared amount of items from the start. It should be slightly better performance-wise if you know how many elements you will add, but I've never tested it. Once you start "exceeding the initial bounds" by adding excess objects, the array resizes itself on the fly. Just using -init and adding elements on demand is OK too.

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.