您的位置:首页 > 产品设计 > UI/UE

UI中初级数据存储

2015-12-08 14:16 363 查看
NSString *homePath=NSHomeDirectory();

    NSLog(@"homePath=%@",homePath);

#pragma marl ~~~~~~~~~~获取沙盒中子文件夹路径的方式一---拼接路径~~~~~

    //获取Documents文件夹路径

    NSString *document = [homePath
stringByAppendingPathComponent:@"/Documents"];

    NSLog(@"document==%@",document);

    

    NSString *library=[homePath
stringByAppendingPathComponent:@"/Library"];

    NSLog(@"library=%@",library);

#pragma mark ~~~~~~~获取沙盒中子文件夹路径的方式二---通过函数获取

    //获取Documents文件夹路径

    NSString *doucumentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES)
lastObject];

    NSLog(@"Documents==%@",doucumentPath);

    //获取library路径

    NSString *libraryPath=[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
NSUserDomainMask, YES)lastObject];

    NSLog(@"libraryPath==%@",libraryPath);

    //获取Cache路径

    NSString *Cache=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
NSUserDomainMask, YES)lastObject];

   // NSLog(@"Cache==%@",Cache);

    //获取temp路径

    NSString *temp=NSTemporaryDirectory();

    NSLog(@"temp==%@",temp);

    //应用程序路径

    NSString *appPath=[[NSBundle
mainBundle] resourcePath];

    NSLog(@"appPath======%@",appPath);

    

    

#pragma mark~~~~~~~~简单文件的写入/读取

    /*

    
简单对象:NSString NSArray NSDictionary NSData

     

    
数据持久化:把简单数据写入到文件中,存储到沙河文件夹里

     

     NSData:用于存储二进制数据

     */

    

    

#pragma mark~~~~~~~~~字符串对象的
写入
和 读取

   
//第一步:创建字符串对象

    NSString *shi =
@"   静夜思\n
床前明月光,\n
疑是地上霜,\n 举头望明月,\n
低头思故乡。";

   
//第二步:构造字符串存储路径

    NSString *strPath = [document
stringByAppendingPathComponent:@"/text.txt"];

   
//第三部:将文件写入指定路径

    [shi writeToFile:strPath
atomically:YES
encoding:NSUTF8StringEncoding
error:nil];

    NSLog(@"strPath==%@",strPath);

    

//读取txt

    NSString *readText=[NSString
stringWithContentsOfFile:strPath encoding:NSUTF8StringEncoding
error:nil];

    NSLog(@"readText=%@",readText);

  

#pragma mark~~~~~~~~~~数组对象的
写入和
读取

   
//构造一个数组对象

    NSMutableArray *starArray=[NSMutableArray
arrayWithObjects:@"Kitty",@"Tom"@"Eid",@"Jack",@"Ilice",
nil];

   
//第二步:构建数组存储路径,创建.plist文件

    NSString *arrayPath=[document
stringByAppendingPathComponent:@"/array.plist"];

    //将数组写入plist

    [starArray writeToFile:arrayPath
atomically:YES];

    NSLog(@"arrayPath=%@",arrayPath);

//数组中再添加新元素

    [starArray addObject:@"baby"];

    [starArray writeToFile:arrayPath
atomically:YES];

    NSLog(@"arrayPath==%@",arrayPath);

    

//读取

    NSArray *readArray=[NSArray
arrayWithContentsOfFile:arrayPath];

    NSLog(@"%@",readArray);

#pragma mark ~~~~~~~~~~~~字典对象的写入

    //构造一个字典 
语法堂 
前面是key  后面是value

    NSDictionary *dic=@{@"name":@"Kitty",@"age":@"18",@"sex":@"girl",@"hobby":@[@"sleep",@"eat"]};

   
//第二步:构造存储字典的plist文件的存储路径

    NSString *dicPath = [document
stringByAppendingPathComponent:@"dic.plist"];

    //第三部:写入

    [dic writeToFile:dicPath
atomically:YES];

    NSLog(@"dicPath=%@",dicPath);

    //第四步:读取

    NSDictionary *readDic=[NSDictionary
dictionaryWithContentsOfFile:dicPath];

    NSLog(@"readDic=%@",readDic);

    

#pragma mark~~~~~~~~NSData对象的写入和读取

   
//第一步:创建图片

    UIImage *image = [UIImage
imageNamed:@"head_19.jpg"];

   
//第二步:将图片转化为二进制

    NSData *imageData=UIImageJPEGRepresentation(image,
0.5);

    //构造NSData文件.da的存储路径

    NSString *imagePath=[document
stringByAppendingPathComponent:@"/data.da"];

    //第四步:写入

    [imageData writeToFile:imagePath
atomically:YES];

    NSLog(@"imagePath=%@",imagePath);

//读取

    UIImage *readImage=[UIImage
imageWithContentsOfFile:imagePath];

    NSLog(@"%@",readImage);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ui 数据存储 nsstring