UserDefaultes 数据存储使用

#import "RootViewController.h"

@interface RootViewController ()
@end

@implementation RootViewController
{
     UILabel *txtInteger;
     UILabel *txtFloat;
     UILabel *txtDouble;
     UILabel *txtNSString;
     UILabel *txtNSDate;
     UILabel *txtNSArray;
     UILabel *txtNSDictionary;
}

 

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self  creatLable];
    [self creatclearbtn];
    [self  saveNSUserDefaults];
    [self  readNSUserDefaults];
}


//保存数据到NSUserDefaults
-(void)saveNSUserDefaults
{
    
    int myInteger = 1000;
    float myFloat = 60.0f;
    double myDouble = 30.0;
    
    NSString *myString = @"我的字符";
    NSDate *myDate = [NSDate date];
    NSArray *myArray = [NSArray arrayWithObjects:@"hello", @"world",@"one",@"dream", 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:@"myString11"];
    [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"];
    txtInteger.text = [NSString stringWithFormat:@"整形:%d",myInteger];
    
    //读取浮点型float类型的数据
    float myFloat = [userDefaultes floatForKey:@"myFloat"];
    txtFloat.text = [NSString stringWithFormat:@"浮点型:%f",myFloat];
    
    //读取double类型的数据
    double myDouble = [userDefaultes doubleForKey:@"myDouble"];
    txtDouble.text = [NSString stringWithFormat:@"%f",myDouble];
    
    //读取NSString类型的数据
    NSString *myString = [userDefaultes stringForKey:@"myString11"];
    txtNSString.text = myString;
    
    //读取NSDate日期类型的数据
    NSDate *myDate = [userDefaultes valueForKey:@"myDate"];
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    txtNSDate.text = [NSString stringWithFormat:@"日期:%@",[df stringFromDate:myDate]];
    
    //读取数组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);
    }
    txtNSArray.text = myArrayString;
    
    //读取字典类型NSDictionary类型的数据
    NSDictionary *myDictionary = [userDefaultes dictionaryForKey:@"myDictionary"];
    NSString *myDicString = [NSString stringWithFormat:@"name:%@, age:%d",[myDictionary valueForKey:@"name"], [[myDictionary valueForKey:@"age"] integerValue]];
    txtNSDictionary.text = myDicString;
}

//清除数据UserDefaultes
-(void)ClearUserDefaultes
{
    NSString   *appDomain=[[NSBundle   mainBundle]bundleIdentifier];
    [[NSUserDefaults  standardUserDefaults]removePersistentDomainForName:appDomain];
    [self  readNSUserDefaults];

}


//清除按钮
-(void)creatclearbtn
{
    UIButton  *btn=[UIButton  buttonWithType:UIButtonTypeRoundedRect];
    btn.frame=CGRectMake(100, 400, 100, 30);
    [btn setTitle:@"清除" forState:UIControlStateNormal];
    btn.backgroundColor=[UIColor  yellowColor];
    [self.view  addSubview:btn];
    [btn  addTarget:self action:@selector(ClearUserDefaultes) forControlEvents:UIControlEventTouchDown];
}

//创建Label
-(void)creatLable
{
    txtInteger=[[UILabel  alloc]initWithFrame:CGRectMake(0, 40, 200, 30)];
    txtInteger.backgroundColor=[UIColor  brownColor];
    [self.view  addSubview:txtInteger];
    
    txtFloat=[[UILabel  alloc]initWithFrame:CGRectMake(0,80, 200, 30)];
    txtFloat.backgroundColor=[UIColor  brownColor];
    [self.view  addSubview:txtFloat];
    
    txtDouble=[[UILabel  alloc]initWithFrame:CGRectMake(0, 120, 200, 30)];
    txtDouble.backgroundColor=[UIColor  brownColor];
    [self.view  addSubview:txtDouble];
    
    txtNSString=[[UILabel  alloc]initWithFrame:CGRectMake(0, 160, 200, 30)];
    txtNSString.backgroundColor=[UIColor  brownColor];
    [self.view  addSubview:txtNSString];
    
    txtNSDate=[[UILabel  alloc]initWithFrame:CGRectMake(0, 200, 200, 30)];
    txtNSDate.backgroundColor=[UIColor  brownColor];

    [self.view  addSubview:txtNSDate];
    
    txtNSArray=[[UILabel  alloc]initWithFrame:CGRectMake(0, 240, 200, 30)];
    txtNSArray.backgroundColor=[UIColor  brownColor];
    [self.view  addSubview:txtNSArray];
    
    txtNSDictionary=[[UILabel  alloc]initWithFrame:CGRectMake(0, 280, 200, 30)];
    txtNSDictionary.backgroundColor=[UIColor  brownColor];
    [self.view  addSubview:txtNSDictionary];

}

 

 

 

 

posted @ 2014-05-05 17:29  huluo666  阅读(296)  评论(0编辑  收藏  举报