How for in loop works internally - Objective C - Foundation

為{幸葍}努か 提交于 2019-12-19 03:40:58

问题


I found this answer:

https://stackoverflow.com/a/5163334/1364174

Which presents how for in loop is implemented.

NSFastEnumerationState __enumState = {0};
id __objects[MAX_STACKBUFF_SIZE];
NSUInteger __count;
while ((__count = [myArray countByEnumeratingWithState:&__enumState objects:__objects count:MAX_STACKBUFF_SIZE]) > 0) {
    for (NSUInteger i = 0; i < __count; i++) {
        id obj = __objects[i];
        [obj doSomething];
    }
}

The problem is that, I found it wrong.

First of all, when you have Automatic Reference Counting (ARC) turned on, you got an error

Sending '__strong id *' to parameter of type '__unsafe_unretained_id*' changes retain/release properties of pointer

But even when I turn ARC off I found out that I __object array seems to behave strangely :

This is actual Code (I assumed MAX_STACKBUFF_SIZE to be 40):

@autoreleasepool {

        NSArray *myArray = @[@"a", @"b", @"c", @"d", @"e", @"f", @"g"];
        int MAX_STACKBUFF_SIZE = 40;
        NSFastEnumerationState __enumState = {0};
        id __objects[MAX_STACKBUFF_SIZE];
        NSUInteger __count;
        while ((__count = [myArray countByEnumeratingWithState:&__enumState objects:__objects count:MAX_STACKBUFF_SIZE]) > 0) {
            for (NSUInteger i = 0; i < __count; i++) {
                id obj = __objects[i];
                __enumState.itemsPtr
                NSLog(@" Object from __objects ! %@", obj);  // on screenshot different message

            }
        }

    }
    return 0;

I got EXC_BAD_ACESS when I try to get the contents of the __object array. I also found out that when you try to iterate through __enumState.itemsPtr it actually works.

Could you explain me what is going on here ? Why my __objects seems to be "shrunken down". And why it doesn't contains desired object? And why is that error when ARC is turned on.

Thank you very very much in advance for your time and effort! (I provided screenshot for better understanding what causes an error)


回答1:


First of all, strong pointers cannot be used in C-structures, as explained in the "Transitioning to ARC Release Notes", therefore the objects array has be be declared as

__unsafe_unretained  id __objects[MAX_STACKBUFF_SIZE];

if you compile with ARC.

Now it is not obvious (to me) from the NSFastEnumeration documentation, but it is explained in Cocoa With Love:Implementing countByEnumeratingWithState:objects:count: that the implementation need not fill the supplied objects array, but can just set __enumState.itemsPtr to an existing array (e.g. some internal storage). In that case, the contents of the __objects array is undefined, which causes the crash.

Replacing

id obj = __objects[i];

by

id obj = __enumState.itemsPtr[i];

gives the expected result, which is what you observed.

Another reference can be found in the "FastEnumerationSample" sample code:

You have two choices when implementing this method:

1) Use the stack based array provided by stackbuf. If you do this, then you must respect the value of 'len'.

2) Return your own array of objects. If you do this, return the full length of the array returned until you run out of objects, then return 0. For example, a linked-array implementation may return each array in order until you iterate through all arrays.

In either case, state->itemsPtr MUST be a valid array (non-nil). ...



来源:https://stackoverflow.com/questions/20724712/how-for-in-loop-works-internally-objective-c-foundation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!