Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > NSUserDefaults behavior

NSUserDefaults behavior
Thread Tools
Dedicated MacNNer
Join Date: Jun 2000
Location: Dundas, Ontario, Canada
Status: Offline
Reply With Quote
Sep 20, 2001, 08:10 PM
 
Hello,

In a game I am writing I am currently thinking about using NSUserDefaults to implement a "high score" screen. In my testing, however, I am running into a problem.

My implementation involves making an NSMutableDictionary that contains a key for each player with an object that is another NSMutableDictionary that contains key value pairs for wins, losses, and ties.

When creating a new player dictionary, I am trying to retrieve the parent (dictionary that encompasses the individual score dictionaries from the default center and then inserting another pair into it. Whenever I try to do that, I get a signal 6 crash on the line that would add the pair.

Any ideas why this is a problem?
Jeff.
Spectral Class
"Shedding Light on Innovation"
     
Junior Member
Join Date: Mar 2001
Status: Offline
Reply With Quote
Sep 20, 2001, 11:45 PM
 
When you retrieve a dictionary from NSUserDefaults, it comes out as an (immutable) NSDictionary even if you saved it as an NSMutableDictionary. Use mutableCopy or an equivalent method to get an NSMutableDictionary from the NSDictionary, and all should be ok.

-Peter
     
Forum Regular
Join Date: Oct 2000
Location: Portland, OR USA
Status: Offline
Reply With Quote
Sep 21, 2001, 03:05 PM
 
I think that the way that NSUserDefaults works, it will manage the preferences per user. You might want to check out this article on O'Reilly.
     
Dedicated MacNNer
Join Date: Jun 2000
Location: Dundas, Ontario, Canada
Status: Offline
Reply With Quote
Sep 21, 2001, 03:15 PM
 
I did end up deciding against an NSUserDefaults implementation in favour of an internal file implementation. The point raised by Wixar seems as though it is probably the cause of my problem. The point by LordJavaC, however, did make me realize something else about this. I was going to put this scores file in the user's library (since I don't want the score board to be copied with the game) but then I realized that this means each user will have a different high scores list which is completely contrary to the point. Instead, I am going to put a file inside the program bundle that will hold the data (and luckily there are API methods to read/write dictionaries from/to files). Although this means that the scores will move between systems, that seems realistic enough to me.

Thanks for your help, guys.

Jeff.
Spectral Class
"Shedding Light on Innovation"
     
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status: Offline
Reply With Quote
Sep 21, 2001, 06:06 PM
 
Instead, I am going to put a file inside the program bundle that will hold the data (and luckily there are API methods to read/write dictionaries from/to files).
Ah, but what if the user running the game doesn't have permission to write to the application bundle? Then their high scores can't be saved. If there was a persistent place on the system where all users had read/write access, you could save it there, but I know of no such place.

One thing you could do is create a world-readable/writable scores file somewhere in /Library (or /Network/Library if it exists) the first time the game is run. If the game is first run as a user who doesn't have permission to those areas, you could use the Security framework to present an authorization panel (so the unprivileged user can call an admin over to their desk). Or you could just save the user's scores to their own domain for the time being; once an admin runs the game and creates the global scores file, the next time the unprivileged user runs the game it could move her scores into the global file.

Another option might be to save world-readable high scores on a per-user basis. To show your global high scores list, you'd look at every user's scores file. You might not even need to write files directly to do this -- I could be mistaken, but I think NSUserDefaults (or CFPreferences, which it's based on) has facilities for reading other users' preferences. Then again, this approach could also be woefully inefficient, and run into potential problems in terms of discovering where users exist.
Rick Roe
icons.cx | weblog
     
Forum Regular
Join Date: Oct 2000
Location: Portland, OR USA
Status: Offline
Reply With Quote
Sep 24, 2001, 04:15 PM
 
I think that a file in "/Library/Application Support" is the best place since you can set it to have unrestricted access and it is system-wide and system-specific.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:19 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2