0

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!

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

Thanks to both of you. At least I understand. I think though, that my design is flawed, that I have this circumstance, so I'm rethinking this piece. (volatile join tables and multiple selections). I got stuck, and you both helped me get unstuck, so thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.