初级数据持有化-沙盒

来源:互联网 发布:中药软件哪个好使 编辑:程序博客网 时间:2024/05/22 00:36

//获取沙盒主路径

    /*NSString *homePath=NSHomeDirectory();

    NSLog(@"%@",homePath);

    

    

    //获取document:通常用来存放应用程序需要持久化使用的关键数据,比如本地数据库等  iTunes在备份的时候会自动备份此文件夹

    NSString *documentPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSLog(@"%@",documentPath);

    

    

    //library:通常存储用来应用程序运行期间生成的持久化数据,比如用户账户等。应用程序退出后不会被删除,但是 iTunes 备份时不会备份此文件夹.

    NSString *library=[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)firstObject];

    NSLog(@"%@",library);

    

    //cache:运行期间的缓存文件:历史记录等

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

    NSLog(@"%@",cachePath);

    

    //preferences :应用程序的设置:夜间模式等

    

    

    

    //tmp:存放临时文件,应用程序结束后清除

    NSString *tmp=NSTemporaryDirectory();

    NSLog(@"%@",tmp);

    

    

    //Bundle

    //获取当前的应用程序包

    NSBundle *bundle=[NSBundle mainBundle];

    //获取当前包的路径

//    NSString *bundlePath=[bundle resourcePath];

    //或者

    NSString *bundlePath=[bundle bundlePath];

    NSLog(@"%@",bundlePath);

    

    //获取包内的文件内容

    NSString *imgPath=[bundle pathForResource:@"dog" ofType:@"png"];

    NSLog(@"%@",imgPath);

    

    NSString *interfacePath=[bundle pathForResource:@"蓝鸥接口文档" ofType:@"txt"];

    NSLog(@"%@",interfacePath);*/

    

    

    //写入文件

    

    //简单对象的写入文件  :数组,字典,字符串,NSData

    

    //字符串写入文件

    /* NSString  *incantation=@"I love you very much.Do you love me?";

    

    NSString *path=NSHomeDirectory();

    path=[path stringByAppendingString:@"/zgc.txt"];

    

    //写入文件的方法

    [incantation writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];

    

    

    //从文件获取文件

    NSString *resultString=[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

    NSLog(@"%@",resultString);*/

    

    //数组写入文件

    /*NSArray *array=@[@"项链想念被相亲",@"一生一世一双人"];

    

    NSString *path=NSHomeDirectory();

    path =[path stringByAppendingString:@"/王安石.txt"];

    //写入文件

    [array writeToFile:path atomically:YES];

    

    //读取文件

    NSArray *result=[NSArray arrayWithContentsOfFile:path];

    NSLog(@"%@",result[0]);*/

    

    //字典写入文件

    /*NSDictionary *dict=@{@"周哥问:":@"我漂亮吗?",

                         @"我答:":@"NO"};

    

    NSString *path=NSHomeDirectory();

    path =[path stringByAppendingString:@"/周哥.txt"];

    //写入文件

    [dict writeToFile:path atomically:YES];

    

    NSDictionary *dic=[NSDictionary dictionaryWithContentsOfFile:path];

    NSLog(@"%@",dic);*/

    

    //图片写入文件


    /*UIImage *image=[UIImage imageNamed:@"dog.png"];

    

    NSData *data=UIImageJPEGRepresentation(image, 1);

    

    NSString *path=NSHomeDirectory();

    path=[path stringByAppendingString:@"/图片.png"];

    //写入文件

    [data writeToFile:path atomically:YES];

    

    //取出图片

    NSData *result=[NSData dataWithContentsOfFile:path];

    UIImage *resultImage=[UIImage imageWithData:data];*/

    

    

    

#pragma mark  --复杂对象写入文件-----

    

    /*Person *person=[Person new];

    person.name=@"MisserSar";

    person.gender=@"man";

    

    

    NSString *path=NSHomeDirectory();

    path=[path stringByAppendingString:@"/person.txt"];

    

    

    //创建可变data,用来存放归档数据

    NSMutableData *mutData=[[NSMutableData alloc]initWithCapacity:10];

    

    //创建归档对象

    NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc]initForWritingWithMutableData:mutData];

    

    //归档

    [archiver encodeObject:person forKey:@"person"];

    //完成归档

    [archiver finishEncoding];

    //写入文件

    [mutData writeToFile:path atomically:YES];

    

    

    

    //使用data对象获取数据

    NSData *data=[NSData dataWithContentsOfFile:path];

    

    //创建反归档对象

    NSKeyedUnarchiver *unchiver=[[NSKeyedUnarchiver alloc]initForReadingWithData:data];

    

    //反归档

    Person *person1= [unchiver decodeObjectForKey:@"person"];

    //完成反归档

    [unchiver finishDecoding];

    

    NSLog(@"%@,%@",person.name,person1.name);*/

    

    

    

    

    //数据持久化的方式

    

    //1. 属性列表

    //2. NSUserDefaults

    //3. write to file

    //4. sqlite

    //5. Core data


0 0