Reputation: 50800
I want to add string objects to an array inside for loop. Below is my code;
for (int i=0;i<[retrievedArray count];i++)
{
NSString *temp = [[retrievedArray objectAtIndex:i] OfficialName];
[detailPgArray addObject:temp];
}
I have 10 objects inside retrievedArray (not as direct strings, but as OfficialName)
But for some reason, at the end of the for loop, detailPgArray has 0 objects. Please help.
Upvotes: 0
Views: 148
Reputation: 10265
In Objective-C you can send a message to nil
and the application won't crash -- and that is what's happening in your code with that addObject
call. You have to alloc and init the array.
Upvotes: 0
Reputation: 57179
detailPgArray is more than likely nil. You need to allocate somewhere. If it is an instance variable try the following.
[detailPgArray release];
detailPgArray = [[NSMutableArray alloc] initWithCapacity:[retrievedArray count]];
for (int i=0;i<[retrievedArray count];i++)
{
NSString *temp = [[retrievedArray objectAtIndex:i] OfficialName];
[detailPgArray addObject:temp];
}
Upvotes: 3
Reputation: 237110
Most likely, detailPgArray
is nil. You need to create an array to go in the variable.
Upvotes: 0
Reputation: 57149
I'd guess that you neglected to allocate and initialize detailPgArray. If it's nil, then your -addObject: calls will go merrily into the void and any later calls to -count will return nil or 0.
Upvotes: 1