Friday, August 6, 2010

Copy-Paste Code: Opening up Settings.plist

So you've bundled a Settings.plist, and now you want to know why you can't edit it? It's because it's not in a writable directory. Let's put that baby where she belongs:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

settingsPlistPath = [[documentsDirectory stringByAppendingPathComponent:@"Settings.plist"] copy];



NSFileManager *fileManager = [[NSFileManager alloc] init];



if (![fileManager fileExistsAtPath:settingsPlistPath]) {

    NSString *bundledSettings = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"plist"];

    [fileManager copyItemAtPath:bundledSettings toPath:settingsPlistPath error:NULL];

}



self.settingsDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:settingsPlistPath];

A Quick Way to Syntax Highlight Code

How to make code pretty before posting: it's easier than you think

Some people would prefer spending the time to set up a script that will highlight their code for them. There definitely advantages to this approach. Not wanting to spend the time and resources to set something like this up (as simple as it seemed to me to be), I opted for a google search: syntax highlight objective-c code online. There are plenty of options out there, but http://quickhighlighter.com/ is the one I liked the best. It has one drawback: you have to copy and paste two bits of HTML into your blog instead of one (it separates the css).

There are plenty of options to choose from, and you can even copy and paste the highlighted text into blogger, but (at least with Google Chrome) you loose the pretty gray box the code comes in.

Monday, August 2, 2010

Finding Memory Leaks - the easy way

From the iPhone Development Guide, "Debugging Applications"


Finding Memory Leaks

If you fix a leak and your program starts crashing, your code is probably trying to use an already-freed object or memory buffer. To learn more about memory leaks, see "Finding Memory Leaks".

You can use the NSZombieEnabled facility to find the code that accesses freed objects. When you turn on NSZombieEnabled, your application logs accesses to deallocated memory, as shown here:

2008-10-03 18:10:39.933 HelloWorld[1026:20b] *** -[GSFont ascender]: message sent to deallocated instance 0x126550

To activate the NSZombieEnabled facility in your application:

  1. Choose Project > Edit Active Executable to open the executable Info window.
  2. Click Arguments.
  3. Click the add (+) button in the “Variables to be set in the environment” section.
  4. Enter NSZombieEnabled in the Name column and YES in the Value column.
  5. Make sure that the checkmark for the NSZombieEnabled entry is selected.
For more information about configuring executable environments, see “Configuring Executable Environments” in Xcode Project Management Guide.

iPhone 3.1 SDK introduces the Leaks instrument, which you can use in the Instruments application to easily find memory leaks. For more information, see Instruments User Guide.

© 2010 Apple Inc. All Rights Reserved. (Last updated: 2010-05-28)