2/15/2009

Differences Between the Simulator and Device

If you've done a bit of iPhone development, you've probably run into scenarios where the simulator and your device behave differently. Generally speaking, it's because the simulator uses OSX's libray (I think) and is a bit more "friendly" to errors, whereas the iPhone is missing a couple things here and there and is much more picky about errors. Case in point - comparing strings. With the simulator, you'll find that the following statement probably works fine:

if (myTestString == @"Blah blah blah") dosomethingcool;

However, the device will balk and possibly crash. What you're supposed to do, and the thing that works on the iPhone, is:

if ([myTestString isEqualToString:@"Blah blah blah"]) dosomethingcool;

In another example, you'll see my entry below about NSUserDefaults. My pro-tip there is that loading an NSMutableArray results in an immutable (uneditable) array, but only on the device. In the simulator, I was able to make changes and everything worked OK (it seemed anyway). But on the device things were effed. So the lesson is: test on your device early and often.

As a final note, and something that I'm beginning to find very useful for a number of reasons, is a little app called AppKiDo. Basically, it lets you quickly browse through the different foundations and included methods and check out their properties, methods and so on and so forth. So, you can look up NSString and easily see what sorts of things you can do with it, or look up AVAudioPlayer and see what its properties are. You can get all this from the docs, but AppKiDo makes it way easier and faster to do so, in my opinion. And since you can have it work specifically for the iPhone, you can more easily find out if a piece of code that you found online is actually supported or not.

No comments:

Post a Comment