Advanced yet simple way to save to NSUserDefaults

Storing user level application settings, UI states, generic counters in an iOS app is an everydayer. The usual – and told to be easiest – way is to store these values is save to NSUserDefaults. But it is not that fluent for everyday use as it should be.

Normally you don’t want to declare each NSUserDefaults key value pair every time you want to store some states, nor implement <NSCoding> hooks one by one. Therefore I made a base class to simplify storing any kind of model object in NSUSerDefaults to the limits, where the only thing to do is to define a model object. Period.

All the saving, restoring, observing is just happen breathtakingly silent under the hood.

The simplest way to save to NSUserDefaults

start using eppz!settings at

Using EPPZUserDefaults to store application states to NSUSerDefaults goes like this.

That’s all, you’re done. Any time you set any property of this object it will be archived into a corresponding NSUserDefault value. If you inspect your stored user defaults in application’s Library/Preferences folder, you will find the object nicely laid out.

Advanced yet simple way to save to NSUserDefaults

Once you defined the object EPPZUserDefaults will take care of sync every NSUserDefaults compilant properties during it’s lifetime.

Default values to NSUserDefaults and other customizations

To keep it clean, EPPZUserDefaults looks for default values in a .plist file named after the class (optional).

Advanced yet simple way to save to NSUserDefaults

The EPPZSettings object defined above takes this .plist file as default values.

As you probably will have some runtime only properties often in these settings objects, there is a class method persistableProperies where you can define keys you want to save. In the testbed project you can find another persisted object EPPZTapCounts that stores only one property lifeTimeTapCount of it’s own.

Advanced yet simple way to save to NSUserDefaults

You can always check the saved values by keep your application’s settings file open during runtime. The EPPZTapCounts object representation stores only the sole property claimed in persistableProperties implementation.

I can think of some future feature whereas you can reset to default values, maintain a singleton instance only of each settings object, other store types like NSKeyedArchiver or even save to the keychain to persist data between application installs, whatnot. But for my current needs, the present features are just fits perfect.

2 thoughts on “Advanced yet simple way to save to NSUserDefaults

  1. bubu

    Excelent library men..

    I’m using and works great, but I have a problem. When I tried to use iPhone/iPad Retina (64 bits), the compiler return me an error: “… EPPZUserDefaults.m:125:65: Incompatible pointer types passing ‘NSUInteger *’ (aka ‘unsigned long *’) to parameter of type ‘unsigned int *’”. This happen on line 125 of EPPZUserDefaults.m, and the app crash. Do you know how to fix this?

    Regards

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">