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 in java?

NSUserDefaults in java?
Thread Tools
Forum Regular
Join Date: Sep 2000
Location: Ronneby, Sweden
Status: Offline
Reply With Quote
Oct 27, 2000, 09:44 AM
 
Can someone point me to an example of using the NSUserDefaults class with java. I don't understand how to create a domain with it. From the docs on apple webpage it seams like the name of the domain is the Bundle Identifier, what is that and how do I change it?

Thanks,
Gabbe
     
ali
Forum Regular
Join Date: Sep 2000
Status: Offline
Reply With Quote
Oct 27, 2000, 12:38 PM
 
Assuming you want to store per-user settings for your app, this is very easy.

The bundle identifier is set in the CFBundleIdentifier key in the Info.plist file, which is part of your app bundle package. This is most easily set through ProjectBUilder's "Application Settings" pane (when inspecting the target). You will want to choose something unique --- use the java package name style, something like com.companyname.appname.

Note that if you don't specify this, things should work by default. The app name will be used as the domain name. However, this might increase the chance of conflicts.

The Game.java class in the BlastApp example (/Developer/Examples/Java/AppKit/BlastApp) uses NSUserDefaults to store preferences and highscore settings.

Ali


     
Forum Regular
Join Date: Sep 2000
Location: Ronneby, Sweden
Status: Offline
Reply With Quote
Oct 28, 2000, 10:37 PM
 
Thanks, that really helped. So now I know how to write and read preferences. Now I have another pretty fundamental question, how do I "catch" events? Let's say I want to save my preferences when the user quits my application or that I want to quit my application when the user closes the main window, how do I do that?

I think it would be pretty logical that the window had a close-outlet or something. Where do I find these events?

/Gabbe
     
ali
Forum Regular
Join Date: Sep 2000
Status: Offline
Reply With Quote
Oct 30, 2000, 11:11 AM
 
Originally posted by gabrielf:
Thanks, that really helped. So now I know how to write and read preferences. Now I have another pretty fundamental question, how do I "catch" events? Let's say I want to save my preferences when the user quits my application or that I want to quit my application when the user closes the main window, how do I do that?

I think it would be pretty logical that the window had a close-outlet or something. Where do I find these events?

/Gabbe
The "events" your describing are available in many cases as "delegate" methods. Some objects (for instance, NSWindow or NSApplication) have delegates, and on certain important events, they consult or notify their delegates. Typically these are things that can be accomplished by subclassing, but often another object is better suited for the task. For instance, when a window is being closed, it makes sense that the window asks the document object (that is, the object which holds state of the document) whether it's OK to close or not.

Note that in addition to delegation, there is also "notification". Delegates are usually consulted for info, and the delegate might intervene in the process (for instance, the delegate could prevent a window from being closed). Notifications are like news reports; they allow multiple observers to listen for changes without directly influencing the outcome. Rather than intervening in the process, observers usually do some additional work upon hearing that something's going down --- for instance, once a window asks its delegate whether it's OK to close down, and the delegate responds favorably, the window then sends out a notification that it's closing. Any observer registered to hear this will get the notification.

Use setDelegate: to set the delegate of an object. See "delegate" methods in header files or documentation (For instance, NSwindow delegate methods are in NSWindow.h or with NSWindow documentation). Use the NSNotificationCenter class to register/unregister observers; again see the header of a given class to see what notifications that class sends out.

BTW, the two specific things you want to do:

- save preferences on quit: this is automatically done for you in Cocoa apps
- quit app when last window closed: this will also be automatically done if you implement a NSApplication delegate method to return YES

Ali


[This message has been edited by ali (edited 10-30-2000).]
     
   
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:36 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