Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: A problem of NSUserdefaults



Do you quit the appliocation using xcode button or by command q?
Because the prefs are saved only when the application receive the termination message....


bye
Yorh
On 13/lug/06, at 18:48, Bus Mini wrote:

Hi all,

I have run into a problem when using NSUserdefaults. That is, I did change
the value of NSUserDefaults ,but the next time I run my application, the
preferences is the old one. Followed is my code, could any one help me find
my problem? Thanks very much.




@interface MyObject : NSObject
{
   IBOutlet id myOutlet1;
   IBOutlet id myOutlet2;
   IBOutlet id myOutlet3;
   IBOutlet id myOutlet4;
NSUserDefaults * prefs;

}
- (IBAction)save:(id)sender;
@end

#import "MyObject.h"

@implementation MyObject

- (IBAction)save:(id)sender
{

       [prefs setObject:[myOutlet1 stringValue] forKey:@"FavBook"];

       [prefs setObject:[myOutlet1 stringValue] forKey:@"FavCity"];

       [prefs setObject:[myOutlet1 stringValue] forKey:@"FavColor"];

       [prefs setObject:[myOutlet1 stringValue] forKey:@"FavFood"];
}


- (id)init { [super init]; NSMutableDictionary * defaultPrefs = [NSMutableDictionary dictionary]; [defaultPrefs setObject:@"Learning Cocoa" forKey:@"FavBook"]; [defaultPrefs setObject:@"San Francisco" forKey:@"FavCity"]; [defaultPrefs setObject:@"Red" forKey:@"FavColor"]; [defaultPrefs setObject:@"Mexican" forKey:@"FavFood"];

   prefs = [[NSUserDefaults standardUserDefaults]
retain];
   [prefs
registerDefaults:defaultPrefs];
   return self;
}

- (void)dealloc
{
   [prefs release];
   [super dealloc];
}


- (void)awakeFromNib { [myOutlet1 setStringValue:[prefs stringForKey:@"FavBook"]]; [myOutlet2 setStringValue:[prefs stringForKey:@"FavCity"]]; [myOutlet3 setStringValue:[prefs stringForKey:@"FavColor"]]; [myOutlet4 setStringValue:[prefs stringForKey:@"FavFood"]]; }



@end


--
Yours sincerely
Mini Bus.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/list% 40metagraphics.it


This email sent to email@hidden

_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >A problem of NSUserdefaults (From: "Bus Mini" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.