开发者

Is it more intelligent to use CFMutableArrayRef in this case?

I need to keep track of integer values in an array, and sometimes I need to modify a value in the array.

Right now I do this:

// Read an integer from the array
NSNumber* num = [NSNumber numberWithInteger:someInteger];
[numArray addObject:num];

To update a value:

// Update an integer in the array
NSNumber* num = [numArray lastObject]; // get old
NSInteger numInt = [numInt integerValue]; // convert
NSNumber* newNum = [NSNumber numberWithInteger开发者_如何学编程:numInt + someAddition]; // create new
[numArray removeLastObject]; // remove old
[numArray addObject:newNum]; // set new

This is so bad. So many objects involved and all because NSNumber is immutable. Now I thought maybe I must step down into good old C and use CFMutableArrayRef? Can I store simple NSInteger values in there without all this overhead?


You could use replaceObjectAtIndex: withObject: command:

[numArray replaceObjectAtIndex:[array count]-1 withObject:newNum];

It's better than your method

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜