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
精彩评论