5

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?

Community
  • 1
  • 1
Mike D
  • 4,938
  • 6
  • 43
  • 99
  • 3
    fast enumeration "takes your word" on the class. @awfullyjohn offers the best solution to handle an array with members of unknown classes. There is no implicit filtering happening and could result in calling a method that the receiver cannot handle ... crash – Jesse Black Dec 09 '11 at 22:48

5 Answers5

9

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.

john
  • 3,043
  • 5
  • 27
  • 48
  • The question is more a deeper understanding of Objective-C than actually doing something like this. Also, I have been working C# lately where the type going into a collection can be specified. – Mike D Dec 09 '11 at 22:41
  • 1
    True. I will update my answer, but basically, you beat me to it. – john Dec 09 '11 at 22:42
  • 1
    A small clarification of your post: `NSObject` itself conforms to the `NSObject` protocol, so any object which inherits from the `NSObject` class (i.e., all but a vanishing few) also conforms to the protocol. – jscs Dec 10 '11 at 02:21
  • 1
    @JoshCaswell Good looking out. I just wanted to explain more in depth what was going on beneath the hood, and give an example of possibly buggy, unintended behavior. Perhaps this specific instance is not the most common problem, but it is good to keep in mind what is actually being called to avoid potential crashes, because even a seemingly innocent call like `NSLog()` will not *always* work on untyped or mistyped objects. – john Dec 10 '11 at 07:10
3

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.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
2

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.

Mark Adams
  • 30,776
  • 11
  • 77
  • 77
  • NSNumber isn't being casted as a NSString. Both NSNumber & NSString are descendants of NSObject, of which NSLog calls `description`, like you say. But there is no casting involved. –  Dec 09 '11 at 23:33
  • Oh I see. I guess I just confused casting with code-completion features. As I was writing the example, I received no warnings about messaging `-length` on an instance of `NSNumber`. – Mark Adams Dec 09 '11 at 23:53
  • Ah! Okay… I also now see what you meant. The NSNumber is being casted as a NSString into `string`. I thought you were saying that NSLog was doing the casting (from the mention of `description`). My bad! –  Dec 09 '11 at 23:56
1

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 *
    // ...
}
Reid Ellis
  • 3,996
  • 1
  • 21
  • 17
1

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.

PengOne
  • 48,188
  • 17
  • 130
  • 149
  • 1
    Wouldn't the "most generic" version be `for( id obj in arr)` (since that covers (the tiny number of) objects which don't descend from `NSObject`)? – jscs Dec 10 '11 at 02:23