On 02/10/2009, at 3:57 PM, James Lin wrote:


Thank you for the code snipet, but I am confused at the logic here...

the following code will be executed EVERY time the program runs, right?

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithCapacity:10]; [dictionary setObject: [NSNumber numberWithBool:YES] forKey:@"PIFirstRun"];
        [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
        [dictionary release];
        
so [dictionary setObject: [NSNumber numberWithBool:YES] forKey:@"PIFirstRun"] will run EVERYTIME the program runs? Wouldn't that set my PIFirstRun to YES every time the user launch my application?

In that case....

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"PIFirstRun"] == YES){ [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"PIFirstRun"];
                //first run
                
                [userDefaults setInteger:5 forKey:@"myIngeter"];
}

will reset my "myInteger" to 5 EVERY single time?


What if all i want is to set "myInteger" to 5 the very first time my application lunches and ONCE ONLY?

Sorry for the confusion...thanks for help again...

James


Hi James,

you use -registerDefaults with user defaults to describe your app's 'factory default' settings. So, when your user first starts your app, the factory defaults get set to YES for first run, but then immediately get set to NO. Next time the user starts up your app, the 'factory defaults' have been changed (by your program, as above) and are never written over UNLESS s/he deletes your app's preferences file in his/her library.


You should check out the User Defaults Programming Guide in your Xcode docs

HTH,
Ron
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to