首页 > 代码库 > 9. iOS 编程之 使用 NSUserDefaults 读取、保存应用程序参数

9. iOS 编程之 使用 NSUserDefaults 读取、保存应用程序参数

1、认识一下 NSUserDefaults

平时我们可能要不离开应用程序,直接在应用程序里边设置配置的参数等,这就需要用到 NSUserDefaults,而 NSUserDefaults 是一个单例类,每个应用程序只有一个 NSUserDefaults 对象。

1)获取 NSUserDefaults 实例的方法(Getting the Shared NSUserDefaults Instance)

+ (NSUserDefaults *)standardUserDefaults:返回默认对象。

+ (void)resetStandardUserDefaults:重置用户默认对象。


2)获取默认值(Getting Default Values)

- (NSArray *)arrayForKey:(NSString *)defaultName

- (BOOL)boolForKey:(NSString *)defaultName

- (NSData *)dataForKey:(NSString *)defaultName

- (NSDictionary *)dictionaryForKey:(NSString *)defaultName

- (float)floatForKey:(NSString *)defaultName

- (NSInteger)integerForKey:(NSString *)defaultName

- (id)objectForKey:(NSString *)defaultName

- (NSArray *)stringArrayForKey:(NSString *)defaultName

- (NSString *)stringForKey:(NSString *)defaultName

- (double)doubleForKey:(NSString *)defaultName

- (NSURL *)URLForKey:(NSString *)defaultName


3)设置默认值(Setting Default Values

- (void)setBool:(BOOL)value
         forKey:(NSString *)defaultName

- (void)setFloat:(float)value
          forKey:(NSString *)defaultName

- (void)setInteger:(NSInteger)value
            forKey:(NSString *)defaultName

- (void)setObject:(id)value
           forKey:(NSString *)defaultName

- (void)setDouble:(double)value
           forKey:(NSString *)defaultName

- (void)setURL:(NSURL *)url
        forKey:(NSString *)defaultName

还有好多好多的方法,属性,请参考官方文档。


2、代码示例

@implementation ViewController

- (void)viewDidLoad {

    [superviewDidLoad];

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

}


- (IBAction)save:(id)sender {

    //获取默认配置文件

    NSUserDefaults *userDefaults = [NSUserDefaultsstandardUserDefaults];

    

    [userDefaults setValue:self.userName.textforKey:@"userName"];

    [userDefaults setValue:self.userPassword.textforKey:@"userPassword"];

    [userDefaults setObject:self.phone.textforKey:@"phone"];

    [userDefaults setObject:self.address.textforKey:@"address"];

    

    NSArray *addList =@[self.add1.text,self.add2.text,self.add3.text];

    [userDefaults setValue:addListforKey:@"addList"];

    

    //马上提交保存

    [userDefaults synchronize];

    

    self.show.text =@"set value success";

    //打印沙盒位置,方便查看(可省略)

    NSLog(@"[%@]",NSHomeDirectory());

}


- (IBAction)get:(id)sender {

    NSUserDefaults *userDefaults = [NSUserDefaultsstandardUserDefaults];

    

    NSString *username = [userDefaultsobjectForKey:@"userName"];

    NSString *userpassword = [userDefaultsobjectForKey:@"userPassword"];

    NSString *phone = [userDefaultsobjectForKey:@"phone"];

    NSString *address = [userDefaultsobjectForKey:@"address"];

    NSArray *addlist = [userDefaultsobjectForKey:@"addList"];

    

    [self.userNamesetText:username];

    [self.userPasswordsetText:userpassword];

    [self.phonesetText:phone];

    [self.addresssetText:address];

    [self.add1setText:addlist[0]];

    [self.add2setText:addlist[1]];

    [self.add3setText:addlist[2]];

    

    self.show.text =@"get value success";

}


- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    [self.userNameresignFirstResponder];

    [self.userPasswordresignFirstResponder];

    [self.phoneresignFirstResponder];

    [self.addressresignFirstResponder];

    [self.add1resignFirstResponder];

    [self.add2resignFirstResponder];

    [self.add3resignFirstResponder];

    

}

@end


3、运行结果

技术分享

技术分享

9. iOS 编程之 使用 NSUserDefaults 读取、保存应用程序参数