Monday, March 16, 2009

selector element does not have a valid object type

One of the changes Apple made to simplify coding in Objective-C 2.0 for OS X and the iPhone was the addition of a simple enumeration syntax. Previously you would have written something like: 
for (int i; i<[array count]; i++) {
  [[array objectAtIndex: i] doSomething];
}
But now you can write
for (NSString *string in array) {
    [string doSomething];
}

This is much nicer, but there is a gotcha lying in wait: you can only apply fast enumeration for iterating over Objective-C objects, not primitive types, so the following:
for (int i in array) { 
... 
}
gives you the almost meaningful error "selector element does not have a valid object type". Unfortunately, it places the error message after the closing bracket of the body of the loop, so you may spend some time wondering how a bracket can cause that error...

7 comments:

  1. Ahh! Thanks for this, saved me a good deal of debugging and research!

    ReplyDelete
  2. Solved my problem too. Thanks.

    ReplyDelete
  3. wonderful. Thanks for the help. it saved my time. I am using xcode 4.3 and it helped me :)

    ReplyDelete