• userDefaults


    //
    //  RootViewController.m
    
    #import "RootViewController.h"
    
    @interface RootViewController ()
    {
        NSMutableArray * dataSource;
    }
    @end
    
    @implementation RootViewController
    
    -(void)createUI
    {
        NSArray * arr = @[@"NSNumber数据:",@"integer:",@"float:",@"double:",@"NSString数据:",@"NSDate数据:",@"NSArray数据:",@"NSDictionary数据:"];
        for(int i = 0;i<[arr count];i++)
        {
            UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 70 + 40 * i, 140, 30)];
            label.text = arr[i];
            label.textAlignment = NSTextAlignmentRight;
            [self.view addSubview:label];
            [label release];
        }
        for(int i = 1;i<[arr count];i++)
        {
            UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(145, 70 + 40 * i, 170, 30)];
            label.tag = i;
            label.textAlignment = NSTextAlignmentLeft;
            label.backgroundColor = [UIColor cyanColor];
            [self.view addSubview:label];
            [label release];
        }
    }
    //向userDefaults里面写入数据
    -(void)writeUserDefaults
    {
        NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
        NSInteger i = 100;
        float f = 50.0f;
        double d = 1000.0;
        NSString * string = @"字符串";
        NSArray * arr = @[@"hello",@"world"];
        NSDictionary * dic = @{@"name":@"test",@"age":@"19"};
        NSDate * date = [NSDate date];
        //存入的数据 无论是可变的还是不可变的 读出来以后都是不可变的数据
        
        [defaults setInteger:i forKey:@"int"];
        [defaults setFloat:f forKey:@"float"];
        [defaults setDouble:d forKey:@"double"];
        //基本类型的数据存放在userDefaults以后都会自动转化成NSNumber
        [defaults setObject:string forKey:@"string"];
        [defaults setObject:arr forKey:@"array"];
        [defaults setObject:dic forKey:@"dic"];
        [defaults setObject:date forKey:@"date"];
        //写入磁盘
        [defaults synchronize];
    }
    //读数据
    -(void)readUserDefaults
    {
        NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
        dataSource = [[NSMutableArray alloc]init];
        NSString * intStr = [NSString stringWithFormat:@"%@",[defaults objectForKey:@"int"]];
        NSString * floatStr = [NSString stringWithFormat:@"%f",[defaults floatForKey:@"float"]];
        NSString * doubleStr = [NSString stringWithFormat:@"%lf",[defaults doubleForKey:@"double"]];
        NSString * stringStr = [defaults objectForKey:@"string"];
        
        NSArray * arr = [defaults objectForKey:@"array"];
        NSString * arrStr = [arr componentsJoinedByString:@" "];
        
        NSDictionary * dic = [defaults objectForKey:@"dic"];
        NSString * dicStr = [NSString stringWithFormat:@"name:%@,age:%@",[dic objectForKey:@"name"],[dic objectForKey:@"age"]];
        
        
        NSDate * date = [defaults objectForKey:@"date"];
        NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
        formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
        NSString * dateStr = [formatter stringFromDate:date];
        [formatter release];
        
        [dataSource addObject:intStr];
        [dataSource addObject:floatStr];
        [dataSource addObject:doubleStr];
        [dataSource addObject:stringStr];
        [dataSource addObject:dateStr];
        [dataSource addObject:arrStr];
        [dataSource addObject:dicStr];
        
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.navigationItem.title = @"UserDefualts的用法";
        self.view.backgroundColor = [UIColor brownColor];
        UIBarButtonItem * item = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(pressBarBtn:)];
        self.navigationItem.rightBarButtonItem = item;
        [item release];
        [self createUI];
        
        [self writeUserDefaults];
        [self readUserDefaults];
    
    }
    -(void)pressBarBtn:(id)sender
    {
        //查找沙盒目录的路径
        NSArray * arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString * path = [arr objectAtIndex:0];
        NSLog(@"%@",path);
        
        for(int i = 0;i<[dataSource count];i++)
        {
            UILabel * lable = (UILabel *)[self.view viewWithTag:i + 1];
            lable.text = dataSource[i];
        }
    }
    
    @end
  • 相关阅读:
    python-MongoDB 非关系型数据库
    python-爬虫-Beautifulsoup模块
    python-flask-路由匹配源码分析
    python-flask-配置文件的源码分析
    python-爬虫-selenium模块
    python-flask-请求源码流程
    python-爬虫
    iOS开发——创建你自己的Framework
    手把手教你封装下载管理器
    AFNetwork 作用和用法详解
  • 原文地址:https://www.cnblogs.com/sayimba/p/5672806.html
Copyright © 2020-2023  润新知