Re: Property in class mirrored to user defaults
Re: Property in class mirrored to user defaults
- Subject: Re: Property in class mirrored to user defaults
- From: Alex Zavatone <email@hidden>
- Date: Mon, 19 Jun 2017 16:56:54 -0500
As mentioned, there is NSUserDefaults.
I think that there is a better way that is pretty much built in, but one that
we often ignore. NSCoding.
This is what it does.
Matt Thompson has a nice write up on this here (in Objective-C and Swift), with
a critical observation at the end of the comparison between Core Data and
NSCoding/NSKeyedArchiver.
http://nshipster.com/nscoding/
Core Data NSKeyedArchiver
Persists State Yes Yes
Pain in the Ass Yes No
Once you set it up, you do this to save the data:
[NSKeyedArchiver archiveRootObject:myRootObject oFile:@"/path/to/archive”];
And you can do this on your singleton’s initialization to populate the object:
[NSKeyedUnarchiver unarchiveObjectWithFile:@"/path/to/archive"];
ALSO, the official patron saint of “Why didn’t Apple give us this?”, Nick
Lockwood has created really nice AutoCoding category on NSObject that makes
this automatic, so you don’t have to set it up manually for every property
within your singleton.
https://github.com/nicklockwood/AutoCoding
You’ll also want to pay attention to when you have to change your data format
too:
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Archiving/Articles/compatibility.html#//apple_ref/doc/uid/20001055-BCICFFGE
> On Jun 12, 2017, at 4:04 AM, Jonathan Taylor <email@hidden>
> wrote:
>
> Hi all,
>
> This feels like a very basic question, but one that I have not had any luck
> searching for (maybe I am using the wrong terms?).
>
> At the moment I have properties in a (singleton) class that are bound to UI
> elements. At the moment they have the same automatic values every time the
> app is launched, although the user can change them while it is running. What
> I would like is for the user's previous choice to be remembered when the app
> is next launched. Obviously this would seem to be a job for NSUserDefaults. I
> am not sure quite what the right way to structure things is, though.
>
> All the simple examples I have seen bind to NSUserDefaults and then if the
> program actually wants to know what the value is, it simply accesses the
> values directly on NSUserDefaults. I would prefer not to do that, though (see
> below).
>
> What I ~think~ I would like is to still be able to access the values as
> properties of my class. That seems to me like the natural way, and it would
> seem odd to have two categories of value, some accessed through properties on
> the class, and some accessed some other way via NSUserDefaults. However, if I
> bind the UI elements to the shared user defaults object, is that not what
> will happen? Or is there some way that I can "link" my class and the user
> defaults object, so that the properties are saved in user defaults but I can
> still access them in a fairly seamless way from my class? I do like the idea
> of having the properties (and their types) explicitly declared as part of my
> class, rather than being mysterious objects that only exist in the user
> defaults.
>
> Does anyone have any advice on how I can achieve this, or on how I should be
> thinking about all this differently?
> Thanks
> Jonny
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden