Use NSUserDefaults to persist data between app runs

On the iPhone you generally want your application to start looking like it did when it was last run to give the user the impression your app is ready to pick up where it left off. NSUserDefaults provides an easy way to store your application’s settings between runs. Simply create an instance of NSUserDefaults and give it the key/value pairs you want it to save.

Storing an integer

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger: myIntValue forKey: @"intValueKey"];
[prefs synchronize]; // writes modifications to disk

Reading it back in

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
int myIntValue = [prefs integerForKey: @"intValueKey"];

or more simply

int myIntValue = [[NSUserDefaults standardUserDefaults] integerForKey: @"intValueKey"];

Of course, this would be pretty useless if it only worked for integers. The same basic syntax also works for setObject/objectForKey and some others. Check the documentation for details. I have not tried with complicated compound objects yet, but NSUserDefaults so far I have had success with strings and simple arrays.


I just learned about NSNotificationCenter. Any class in a program can send a notification message to the notification center, and all classes can listen to the center and respond to messages that apply to it. It’s kind of like a callback, but really not.

This is part of my code for implementing a custom keyboard in my iPhone app.

To send a notification named “keyboardDone” to all objects in your program.

[[NSNotificationCenter defaultCenter] postNotificationName: @"keyboardDone" object: nil];

To listen for the “keyboardDone” notification and call the “keyboardDoneObserver:” method when it’s received this can be added to your class’ init code.

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(keyboardDoneObserver:) name: @"keyboardDone" object: nil];

And this is the “keyboardDoneObserver:” method that gets called when notified.

-(void)keyboardDoneObserver: (NSNotification*)notification {
     // do work here

Adding a code beautifier script to Xcode

Xcode’s re-indent command is pretty weak compared to the code reformatting built into other IDEs. It fixes the indentation at the beginning on each line, which makes a huge difference in code readability, but I don’t think it does anything else. In Eclipse and VS I’ve gotten used to being fairly lazy about being consistent with my spacing elsewhere and relying on their built in reformatting to clean it up for me. I wanted to change the way Xcode reformats code, since it’s easier to change tools than to break bad habits.

I spent some time searching for command line code beautifiers that support Objective C and only really found one. Uncrustify is open source, which is pretty fortunate since they don’t have an OS X binary download of the current version. The source download includes the needed project files for Xcode, so compiling your own is easy enough. Just open uncrustify.xcodeproj and compile. You could also download another project, Universal Indent GUI which includes an already compiled Uncrustify executable along with a GUI front end.

After I compiled the executable, I copied it and the defaults.cfg file to a new directory and created a couple user scripts in Xcode to use it. The code for both scripts is identical, just adjust it for the correct paths to the files.

echo -n "%%%{PBXSelection}%%%"
/path/to/uncrustify -q -c /path/to/defaults.cfg
echo -n "%%%{PBXSelection}%%%"

The “Reformat all” script will run Uncrustify on the entire document.

The “Reformat selection” is the same except “Input” is set to “Selection” and “Output” is set to “Replace Selection”.

The defaults.cfg does pretty well with obj-c but isn’t perfect. At least it’s consistent. One of these days I’ll find a better config for it it try fixing it myself.

Yahoo! UI Library

I don’t know if its because of their trying to compete with Google in the cool factor or what, but Yahoo has released some cool libraries and design pattern docs for creating interactive sites. I want to play with it sooo much right now, but I have a midtern in statistics tomorrow. Anyway, even if I don’t use their code I will be using it to learn how to do some cool things for a project I’m trying to figure out right now. Check out the Yahoo! UI Library and the Design Pattern Library.

Why do people switch to Linux?

I just came across an O’Reilly article about the reasons people switch to Linux, and how most people who switched did not do it out of hatred of Microsoft.

I’ve used Linux off and on since around 1996 or 1997, and am currently in one of the rare periods where there is no Linux box anywhere in my house. I think in a lot of ways it comes down to choosing the best tool for the job. In 1998 I built a dual-homed Redhat box to act as my firewall for my home network. At the time you could not just buy a Linksys router for $30 and Linux was a cheaper and better solution that Windows. When I decided I wanted to learn PHP and mySQL, the obvious solution was to stick a Linux box in the closet. When KDE became stable enough to actually use, and quit leaving core dump files all over the system, I started using Linux for my primary desktop. There was a couple year period where I hardly touched a Windows machine outside of work. As a computer science student, most of my labs have to be compiled to run on the schools Solaris machines, and being able to develop on Linux (using KDevelop, by the way) greatly increased my productivity compared to students who used Windows to edit files and had to copy them to the remote Sun box just to try to compile and debug. Right now I’m back to using Windows XP on my primary machine. I’m past most of the coding classes now, and the coding classes I have left are focusing on VB.Net. I’m also taking my communications classes that include assignments requiring specific features in Microsoft Office like tracking changes and embedding charts creating in Excel in Word docs. Right now Windows is the best tool for what I need to do. I still use Thunderbird as my mail client, and was even able to transfer my old messages and settings from my last Linux box to Windows.

So, after next summer I’ll be done with my degree and won’t have that to weigh on my OS decision. What will I choose? I just don’t know. If I’m still playing with c# in my spare time I might stick with Windows XP, or move to Vista. Then again I might just buy a Mac.

Visual Studio Rots the Mind

If you’re a Windows programmer you should know who Charles Petzold is. Ten years ago his Programming Windows book was the best source of information on the win32 API, and now he has written a handful of .Net books. Apparently he’s come to the conclusion that Visual Studio Rots the Mind and his lengthy rant is excellent. Personally I don’t think this is Visual Studio’s fault, especially since in many ways Visual Studio is just keeping up with competitors like Eclipse, but is more a commentary on how current development tools encourage development to follow certain patterns that focus on productivity instead of creativity.