简体   繁体   中英

obj-c array handling - How to add first object?

I am struggling with a noob thing. I have this loop through an initialized array:

for (int i = 0; i < array.count; i++) {

       [array addSomeObject...];
    }

How do I add the first object? The loop is not executed as array.count is 0. I probably deserve to be voted down for this. Just tell me how to deal with it :! Many thanks!

You can't use the array's count to do this. If you have a count that you want the array to have when your finished, use that number in your loop.

Decide how many items you want in the array and use that number as the loop limit - not the count as it is obviously zero. Note that if you create an array with a capacity but don't add anything to it it still has a count of zero. The capacity is just a hint at how much memory should be allocated for the array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM