Note that there are some explanatory texts on larger screens.

plurals
  1. POProper and most efficient way to iterate through an NSMutableSet?
    primarykey
    data
    text
    <p>Some Context: In my destructible terrain library for Cocos2D, one of the features is collapsable terrain. In order to collapse efficiently, I store the columns whose pixels have been altered in a NSMutableSet which contains NSNumbers which contain integers. I am using this data structure because I don't want to iterate over duplicate columns.</p> <p>My first instinct for how to loop through the NSMutableSet was to use a 'for in' loop.</p> <pre><code>for (NSNumber * col in [alteredColumns allObjects]) { // Works // for (NSNumber * col in alteredColumns) { // Crashes int x = col.intValue; bool shouldShift = false; bool alphaFound = false; for (int y = (size_.height -1); y &gt;= 0; y--) { if (!shouldShift) { if ([self pixelAt:ccp(x,y)].a == 0) { // Need to shift all pixels above one down alphaFound = true; } else if (alphaFound) { didCollapse = shouldShift = true; // Ensure the top pixel is alpha'ed out if a collapse will occur [self setPixelAt:ccp(x,0) rgba:ccc4(0, 0, 0, 0)]; [self setPixelAt:ccp(x,(y+1)) rgba:[self pixelAt:ccp(x,y)]]; } // end inner if } else { // Need to shift pixels down one [self setPixelAt:ccp(x,(y+1)) rgba:[self pixelAt:ccp(x,y)]]; } // end if } // end inner for // Remove column from cache if no pixels collapsed if (!shouldShift) [alteredColumns removeObject:col]; } // end outer for </code></pre> <p>However, this led to bad results. The program would crash with a Bad Access Memory error. If I alter the for loop to use [alteredColumns allObjects], then everything works just fine. So Questions... Can a 'for in' loop not be used on an unordered collection such as a NSMutableSet? If I must use a method such as allObjects, is this efficient?</p> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload