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 {

    [super viewDidLoad];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

}


- (IBAction)save:(id)sender {

    //获取默认配置文件

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

    

    [userDefaults setValue:self.userName.text forKey:@"userName"];

    [userDefaults setValue:self.userPassword.text forKey:@"userPassword"];

    [userDefaults setObject:self.phone.text forKey:@"phone"];

    [userDefaults setObject:self.address.text forKey:@"address"];

    

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

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

    

    //马上提交保存

    [userDefaults synchronize];

    

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

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

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

}


- (IBAction)get:(id)sender {

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

    

    NSString *username = [userDefaults objectForKey:@"userName"];

    NSString *userpassword = [userDefaults objectForKey:@"userPassword"];

    NSString *phone = [userDefaults objectForKey:@"phone"];

    NSString *address = [userDefaults objectForKey:@"address"];

    NSArray *addlist = [userDefaults objectForKey:@"addList"];

    

    [self.userName setText:username];

    [self.userPassword setText:userpassword];

    [self.phone setText:phone];

    [self.address setText:address];

    [self.add1 setText:addlist[0]];

    [self.add2 setText:addlist[1]];

    [self.add3 setText:addlist[2]];

    

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

}


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

{

    [self.userName resignFirstResponder];

    [self.userPassword resignFirstResponder];

    [self.phone resignFirstResponder];

    [self.address resignFirstResponder];

    [self.add1 resignFirstResponder];

    [self.add2 resignFirstResponder];

    [self.add3 resignFirstResponder];

    

}

@end


3、运行结果

技术分享

技术分享

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。