码迷,mamicode.com
首页 > 其他好文 > 详细

沙盒机制 归档 反归档

时间:2014-11-20 00:03:23      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:io   ar   使用   sp   for   文件   数据   on   log   

//文件路径

//1.Home主目录:里面有Document, library,tem,和一个应用程序

    NSLog(@"%@",NSHomeDirectory());

    

    //2.Document路径

    NSString *docuPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

    NSLog(@"%@",docuPath);

    

    //3.Library路径

    NSString *libPath=NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask, YES)[0];

    NSLog(@"%@",libPath);

    

    //4.Temp路径

    NSLog(@"%@",NSTemporaryDirectory());

    

    //5.caches路径

    NSString *caches=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];

    NSLog(@"%@",caches);

 

     //6.user路径

    NSString *user=NSUserName();

    NSLog(@"%@",user);

   

     //7.NSBundle路径路径

    NSString *bundle=[[NSBundle mainBundle]pathForResource:@"6" ofType:@"png"];

    NSLog(@"%@",bundle);

 

  //简单文件写入

//NSString写入

    //1.写入的路径

    NSString *s=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

    NSLog(@"%@",s);

    //2.拼接文件路径

    NSString *filePath=[s stringByAppendingString:@"/myText.txt"];

    

    //3.准备写入的内容

    NSString *content=@"hello,world";

    

    //4.写入

    [content writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];

    

    //5.文件读取

    NSString *readString=[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

    NSLog(@"%@",readString);

    

    

//NSArray

    //1.获取document路径

    NSString *s1=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

 

    //2.拼接文件路径

    NSString *arrFile=[s1 stringByAppendingString:@"/array.plist"];

    //3.准备内容

    NSArray *array=@[@"123",@"456",@"678"];

    //4.写入

    [array writeToFile:arrFile atomically:YES];

    //5.读取文件

    NSArray *arr=[NSArray arrayWithContentsOfFile:arrFile];

    NSLog(@"%@",arr);

    

//NSDictionary

    //1.获取写入路径

    NSString *s2=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

 

    //2.拼接文件路径

    NSString *dicFile=[s2 stringByAppendingString:@"/dictionary.plist"];

    //3.准备内容

    NSDictionary *dic=@{@"1":@"a",@"2":@"b",@"3":@"c"};

    //4.写入

    [dic writeToFile:dicFile atomically:YES];

    //5.读取数据

    NSDictionary *d=[NSDictionary dictionaryWithContentsOfFile:dicFile];

    NSLog(@"%@",d);

 

 

***********************************

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

    NSString *path=[documentPath stringByAppendingString:@"/Person7"];

     //创建文件

    [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];

    

    NSLog(@"%@",path);

    //修改文件

    

    NSString *stu=[documentPath stringByAppendingString:@"/student"];

    [[NSFileManager defaultManager] moveItemAtPath:path toPath:stu error:nil];

    

    //删除文件

    [[NSFileManager defaultManager] removeItemAtPath:stu error:nil];

 

 

 

 

 

 

 

*********************************************

归档

1.先在要归档的类里(如Person类.h中)遵循coding协议

2.在(person.m中)遵循协议方法(一个归档,一个反归档)

#define kname @"name"

#define kage @"age"

@implementation Person

//进行归档编码时候码调用(系统调用)

-(void)encodeWithCoder:(NSCoder *)aCoder

{

    //对属性进行编码

    [aCoder encodeObject:self.name forKey:kname];

    [aCoder encodeObject:self.age forKey:kage];

}

//反归档

-(id)initWithCoder:(NSCoder *)aDecoder

{

    self=[super init];

    //反编码

    if (self) {

        self.name=[aDecoder decodeObjectForKey:kname];

        self.age=[aDecoder decodeObjectForKey:kage];

    }

    return self;

}

3.实现归档反归档

*******归档方法一*******

   

    //创建Person类的实例对象

    Person *person1=[[Person alloc] init];

    person1.name=@"张三";

    person1.age=@"39";

    

    Person *person2=[[Person alloc] init];

    person2.name=@"李四";

    person2.age=@"18";

    

    

    //归档使用的NSData

    NSMutableData *person1Data=[NSMutableData data];

    

    //创建归档工具

    NSKeyedArchiver *achiver=[[NSKeyedArchiver alloc] initForWritingWithMutableData:person1Data];

    

    

    //进行归档

    [achiver encodeObject:person1 forKey:kPerson1];

    [achiver encodeObject:person2 forKey:kPerson2];

    

    //完成转换,结束归档

    [achiver finishEncoding];

    

    

    //获取Document

    NSString *person1Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

    //拼接路径

    

    NSString *pPath=[person1Path stringByAppendingString:@"/Person"];

    

    //写入

    [person1Data writeToFile:pPath atomically:YES];

    NSLog(@"%@",person1Path);

 

//反归档

    

    //通过文件路径获取data数据

    NSData *unData=[NSData dataWithContentsOfFile:pPath];

    

    //反归档工具

    NSKeyedUnarchiver *unArchiver=[[NSKeyedUnarchiver alloc] initForReadingWithData:unData];

    

    

    //反归档

    Person *p=[unArchiver decodeObjectForKey:kPerson1];

    Person *p2=[unArchiver decodeObjectForKey:kPerson2];

    //结束反归档

    [unArchiver finishDecoding];

    

    NSLog(@"name:%@",p.name);

    NSLog(@"%@",p2.name);

    *************************************

 

    

****归档方法二*********** 

  //获取 documen路径

    

    NSString *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

    

    NSString *filepath=[path stringByAppendingString:@"/personArray.plist"];

    

    Person *pn1=[[Person alloc] init];

    pn1.name=@"1";

    pn1.age=@"2";

    Person *pn2=[[Person alloc] init];

    pn2.name=@"3";

    pn2.age=@"4";

    

    NSArray *a=@[pn1,pn2];

    

    //归档

    [NSKeyedArchiver archiveRootObject:a toFile:filepath];

    

    //反归档

    

  NSArray *arr=  [NSKeyedUnarchiver unarchiveObjectWithFile:filepath];

    NSLog(@"%@",[arr[0] name]);

    

 

 

沙盒机制 归档 反归档

标签:io   ar   使用   sp   for   文件   数据   on   log   

原文地址:http://www.cnblogs.com/haiying/p/4109470.html

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