码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 归档解档

时间:2016-06-27 19:43:37      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

归档:

    NSMutableData *data = [[NSMutableData alloc] init];

    //创建归档辅助类

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

    //编码

    [archiver encodeObject:@"2009-12-09" forKey:@"dataTime"];

   //结束编码

    [archiver finishEncoding];

    //写入

    [data writeToFile:[self getFilePathWithModelKey:[NSString stringWithFormat:@"test"]] atomically:YES];

 

 

方法://得到Document目录

-(NSString *) getFilePathWithModelKey:(NSString *)modelkey

{

    NSArray *array =  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    return [[array objectAtIndex:0] stringByAppendingPathComponent:modelkey];

}

 

 

解档:

        NSData * data = [[NSData alloc] initWithContentsOfFile:[self getFilePathWithModelKey:[NSString stringWithFormat:@"test"]]];

        //解档辅助类

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

        NSString * time = [unarchiver decodeObjectForKey:@"dataTime"];

        //关闭解档

        [unarchiver finishDecoding];

 

iOS 归档解档

标签:

原文地址:http://www.cnblogs.com/tongyuling/p/5620881.html

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