NSUserDefaults用法

NSString *sessionId = @"123";

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [defaults setObject:sessionId forKey:@"sessionID"];

    [defaults synchronize];

    

    NSString *value;  

    value = [defaults objectForKey:@"sessionID"];  //获取值

 

 

Saving

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

// saving an NSString
[prefs setObject:@"TextToSave" forKey:@"keyToLookupString"];

// saving an NSInteger
[prefs setInteger:42 forKey:@"integerKey"];

// saving a Double
[prefs setDouble:3.1415 forKey:@"doubleKey"];

// saving a Float
[prefs setFloat:1.2345678 forKey:@"floatKey"];

// This is suggested to synch prefs, but is not needed (I didn't put it in my tut)
[prefs synchronize]; //同步,写到磁盘中;

Retrieving

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

// getting an NSString
NSString *myString = [prefs stringForKey:@"keyToLookupString"];

// getting an NSInteger
NSInteger myInt = [prefs integerForKey:@"integerKey"];

// getting an Float
float myFloat = [prefs floatForKey:@"floatKey"];

 

NSUserDefaults只支持: NSString, NSNumber, NSDate, NSArray, NSDictionary.

 
文章来自:http://blog.csdn.net/dong_007_007/article/details/7729205

你可能感兴趣的:(NSUserDefaults)