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...

Sunday, March 15, 2009

assignment from distinct Objective-C type

If you want to overwrite the contents of an Objective C object, you might be tempted to do something like the following:

wordarray = [[NSMutableArray alloc] init];
... interesting things here ...
wordarray = [sentence componentsSeparatedByString:@" "];


You'll find that the second line there gets an enigmatic message "warning:assignment from distinct Objective-C type". If you ignore the message and run the code, everything runs fine so what's the problem? Well, you're stamping all over Objective-C's memory allocation and garbage collection, so at some point in the future, when you're least expecting it, your program will crash dramatically. The safe way to refine the contents of an object is by using a setXXXX: message:

[wordarray setArray:[sentence componentsSeparatedByString:@" "]];

Saturday, March 14, 2009

Unable to read symbols for "/System/Library/Frameworks/UIKit.framework/UIKit"

So you've been playing the XCode and wrapping your head round Cocoa and the Interface Builder, and then you make one inconsequential change, and your whole world falls apart: instead of the gloriously lickable application appearing in the iPhone simulator, the simulator launches and then closes, and XCode reports __TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION__. When you open the debugger window, you see a worrying set of messages such as: 

warning: Unable to read symbols for "/System/Library/Frameworks/UIKit.framework/UIKit" (file not found).
warning: Unable to read symbols from "UIKit" (not yet mapped into memory).
warning: Unable to read symbols for "/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics" (file not found).
warning: Unable to read symbols from "CoreGraphics" (not yet mapped into memory).

"What the hell did I do? I deleted my frameworks?? How???" may be the thoughts going through your head right now - but don't worry! All that is happening is the the gdb debugger is looking in the wrong place for the iPhoneOS frameworks; it's looking in the MacOS framework directory /System/Library/Frameworks instead of the iPhoneOS directory which would be something like /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.1.sdk/System/Library/Frameworks/CoreGraphics.framewor. Unfortunately, these messages take up most of the space on the default tiny gdb window, so the error that caused gdb to be launched has scrolled off the top of the screen. If you scroll up, you'll find the real error that your minor change caused.

I've not found a clear explanation why gdb is using the wrong path, or how to correct this, but if you're desperate to suppress these messages, you can set up symbolic links to the appropriate frameworks e.g.
sudo ln -s /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.1.sdk/System/Library/Frameworks/UIKit.framework/  /System/Library/Frameworks/UIKit.framework 

If anyone knows how to fix gdb's path information, please let me know.