码迷,mamicode.com
首页 > 移动开发 > 详细

iOS数据持久化(一、NSUserDefaults)

时间:2015-08-04 22:35:06      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

  NSUserDefaults适合存储轻量级的本地数据,比如要保存一个登陆界面的数据,用户名、密码之类的,个人觉得使用NSUserDefaults是首选(需要加密的登录信息请略过)。下次再登陆的时候就可以直接从NSUserDefaults里面读取上次登陆的信息咯。

因为如果使用自己建立的plist文件什么的,还得自己显示创建文件,读取文件,很麻烦,而是用NSUserDefaults则不用管这些东西,就像读字符串一样,直接读取就可以了。

  NSUserDefaults支持的数据格式有:NSNumber(Integer、Float、Double),NSString,NSDate,NSArray,NSDictionary,BOOL类型。很实用吧

NSUserDefaults很方便,读取也很容易。下面给出一个示例看看如何使用:

//保存数据到NSUserDefaults
-(void)saveNSUserDefaults
{
    NSString *myString = @"enuola";
    int myInteger = 100;
    float myFloat = 50.0f;
    double myDouble = 20.0;
    NSDate *myDate = [NSDate date];
    NSArray *myArray = [NSArray arrayWithObjects:@"hello", @"world", nil];
    NSDictionary *myDictionary = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"enuo", @"20", nil] forKeys:[NSArray    arrayWithObjects:@"name", @"age", nil]];
    
    //将上述数据全部存储到NSUserDefaults中
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    //存储时,除NSNumber类型使用对应的类型意外,其他的都是使用setObject:forKey:
    [userDefaults setInteger:myInteger forKey:@"myInteger"];
    [userDefaults setFloat:myFloat forKey:@"myFloat"];
    [userDefaults setDouble:myDouble forKey:@"myDouble"];
    
    [userDefaults setObject:myString forKey:@"myString"];
    [userDefaults setObject:myDate forKey:@"myDate"];
    [userDefaults setObject:myArray forKey:@"myArray"];
    [userDefaults setObject:myDictionary forKey:@"myDictionary"];
    
    //这里建议同步存储到磁盘中,但是不是必须的
    [userDefaults synchronize];
    
}

//从NSUserDefaults中读取数据
-(void)readNSUserDefaults
{
    NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
    
    //读取数据到各个label中
    //读取整型int类型的数据
    NSInteger myInteger = [userDefaultes integerForKey:@"myInteger"];
    NSString *myIntegerStr = [NSString stringWithFormat:@"%ld",(long)myInteger];
    NSLog(@"%@",myIntegerStr);
    
    //读取浮点型float类型的数据
    float myFloat = [userDefaultes floatForKey:@"myFloat"];
    NSString *myFloatStr = [NSString stringWithFormat:@"%f",myFloat];
     NSLog(@"%@",myFloatStr);
    //读取double类型的数据
    double myDouble = [userDefaultes doubleForKey:@"myDouble"];
    NSString *myDoubleStr = [NSString stringWithFormat:@"%f",myDouble];
    NSLog(@"%@",myDoubleStr);
    
    //读取NSString类型的数据
    NSString *myString = [userDefaultes stringForKey:@"myString"];
    NSString *myStringStr = myString;
    NSLog(@"%@",myStringStr);
    
    //读取NSDate日期类型的数据
    NSDate *myDate = [userDefaultes valueForKey:@"myDate"];
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *myDateStr = [NSString stringWithFormat:@"%@",[df stringFromDate:myDate]];
    NSLog(@"%@",myDateStr);
    
    //读取数组NSArray类型的数据
    NSArray *myArray = [userDefaultes arrayForKey:@"myArray"];
    NSString *myArrayString = [[NSString alloc] init];
    for(NSString *str in myArray)
    {
        NSLog(@"str= %@",str);
        myArrayString = [NSString stringWithFormat:@"%@  %@", myArrayString, str];
        [myArrayString stringByAppendingString:str];
        //        [myArrayString stringByAppendingFormat:@"%@",str];
        NSLog(@"myArrayString=%@",myArrayString);
    }
    NSString *myArrayStr = myArrayString;
    NSLog(@"%@",myArrayStr);
    
    //读取字典类型NSDictionary类型的数据
    NSDictionary *myDictionary = [userDefaultes dictionaryForKey:@"myDictionary"];
    NSString *myDicString = [NSString stringWithFormat:@"name:%@, age:%ld",[myDictionary valueForKey:@"name"], (long)[[myDictionary valueForKey:@"age"] integerValue]];
    NSString *myDictTionary = myDicString;
    NSLog(@"%@",myDictTionary);
}

 

iOS数据持久化(一、NSUserDefaults)

标签:

原文地址:http://www.cnblogs.com/Zerone-Lee/p/4703244.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!