Fast Enumeration on NSArray of Different Types

感情迁移 提交于 2019-12-28 06:48:10

问题


I have this question here (as well other quesrtions on SO), and the Apple docs about Objective-C collections and fast enumeration. What is not made clear is if an NSArray populated with different types, and a loop is created like:

for ( NSString *string in myArray )
    NSLog( @"%@\n", string );

What exactly happens here? Will the loop skip over anything that is not an NSString? For example, if (for the sake of argument) a UIView is in the array, what would happen when the loop encounters that item?


回答1:


Why would you want to do that? I think that would cause buggy and unintended behavior. If your array is populated with different elements, use this instead:

for (id object in myArray) {
    // Check what kind of class it is
    if ([object isKindOfClass:[UIView class]]) {
       // Do something
    }
    else {
       // Handle accordingly
    }
}

What you are doing in your example is effectively the same as,

for (id object in myArray) {
    NSString *string = (NSString *)object;
    NSLog(@"%@\n", string);
}

Just because you cast object as (NSString *) doesn't mean string will actually be pointing to an NSString object. Calling NSLog() in this way will call the - (NSString *)description method according to the NSObject protocol, which the class being referenced inside the array may or may not conform to. If it conforms, it will print that. Otherwise, it will crash.




回答2:


You have to understand that a pointer in obj-c has no type information. Even if you write NSString*, it's only a compilation check. During runtime, everything is just an id.

Obj-c runtime never checks whether objects are of the given class. You can put NSNumbers into NSString pointers without problems. An error appears only when you try to call a method (send a message) which is not defined on the object.

How does fast enumeration work? It's exactly the same as:


for (NSUInteger i = 0; i < myArray.count; i++) {
    NSString* string = [myArray objectAtIndex:i];

    [...]
}

It's just faster because it operates on lower level.




回答3:


Interesting question. The most generic syntax for fast enumeration is

for ( NSObject *obj in myArray )
    NSLog( @"%@\n", obj );

I believe that by doing

for ( NSString *string in myArray )
    NSLog( @"%@\n", string );

instead, you are simply casting each object as an NSString. That is, I believe the above is equivalent to

for ( NSObject *obj in myArray ) {
    NSString *string = obj;
    NSLog( @"%@\n", string );
}

I could not find precise mention of this in Apple's documentation for Fast Enumeration, but you can check it on an example and see what happens.




回答4:


I just tried a quick example... Here is my code.

NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:1];
NSNumber *number = [NSNumber numberWithInteger:6];
[array addObject:number];
[array addObject:@"Second"];

Now if I simply log the object, no problem. The NSNumber instance is being cast as an NSString, but both methods respond to -description, so its not a problem.

for (NSString *string in array)
{
    NSLog(@"%@", string);
}

However, if I attempt to log -length on NSString...

for (NSString *string in array)
{
    NSLog(@"%i", string.length);
}

... it throws an NSInvalidArgumentException because NSNumber doesn't respond to the -length selector. Long story short, Objective-C gives you a lot of rope. Don't hang yourself with it.




回答5:


Since all NSObject's respond to isKindOfClass, you could still keep the casting to a minimum:

for(NSString *string in myArray) {
    if (![string isKindOfClass:[NSString class]])
        continue;
    // proceed, knowing you have a valid NSString *
    // ...
}


来源:https://stackoverflow.com/questions/8452505/fast-enumeration-on-nsarray-of-different-types

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