On Oct 7, 2009, at 10:47 AM, Marco S Hyman wrote:

On Oct 7, 2009, at 10:33 AM, Steve Christensen wrote:

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?

Yes, because you're modifying the value of that key every time.

No.  Read the code snip again.  setInteger will only be set
when PIFirstRun is YES.  When YES PIFirstRun is set to NO so
future runs will skip setting myInteger.  Unless, as you noted
in comments I snipped, the preferences file is deleted or other
code sets PIFirstRun back to YES.

My mistake.

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

Add it to the defaults dictionary:

NSDictionary* defaults = [NSDictionary dictionaryWithObjectsAndKeys:
                                [NSNumber numberWithBool:YES], @"PIFirstRun",
                                [NSNumber numberWithInt:5],    @"myInteger",
                                nil];

[[NSUserDefaults standardUserDefaults] defaults];

That may also do the trick.   I say MAY because it is possible that
the act of setting the code might cause some side effect that won't
occur if the defaults are initialized to the value.

I suppose if something was bound to that key then some other behavior could be triggered. If the idea was that the value really should be initialized once then putting it in the defaults would be best, unless you wanted to know if the value had been set to some value vs. being undefined.

_______________________________________________

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