开发者

strings not getting added to NSMutableArray

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.


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];
}


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.


Most likely, detailPgArray is nil. You need to create an array to go in the variable.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜