Elementary NSUserDefaults Question
Elementary NSUserDefaults Question
- Subject: Elementary NSUserDefaults Question
- From: Charles Jenkins <email@hidden>
- Date: Wed, 07 Dec 2016 10:25:46 -0500
I may be misusing NSUserDefaults. I want to store the name of a background
music file, which may be nil if the user doesn’t want to hear anything. For
the time being, I have only two settings for my variable
“currentBackgroundMusicFileName”: either a file that I distribute with the
app, or nil.
When the app starts up, we call NSUserDefaults.standard.register( [ “bgm” :
<myfilename> ) to default to the real file name, so the user will hear
music on first run. Then I call NSUserDefaults.standard.object( forKey:
“bgm” ) as? String to read the current setting.
When I run the app, I go into my settings and select “None” as the
background music file I want to hear, and the variable gets changed and we
call NSUserDefaults.standard.set( nil, forKey: “bgm” ). The music is
silenced.
But when I kill the app and start again, the music comes back. Is it it a
mistake to try using nil as an object value? Perhaps object( forKey: ) is
meant to return the registered default value again instead of the nil when
I reload the app, making it impossible for me to use nil as a valid value?
It will be funny if, for a purpose like this, one must define an additional
value that means “none”—exactly the thing Optionals were supposed to
prevent—but not surprising. OTOH, maybe there’s something I’m supposed to
do after setting a value to tell NSUserDefaults I really, really mean for
it to save the changed value?
--
Charles
_______________________________________________
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