测试归档_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > 测试归档

测试归档

 2017/2/23 5:32:52  宁静暖风  程序员俱乐部  我要评论(0)
  • 摘要:#import"ViewController.h"#import"dog.h"@interfaceViewController()@end@implementationViewController//存数据-(IBAction)writeDataAction:(UIButton*)sender{//1.获取存储的路径NSString*documents=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory
  • 标签:测试

class="p1">#import "ViewController.h"

#import "dog.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController

 

//存数据

- (IBAction)writeDataAction:(UIButton *)sender {

    //1.获取存储的路径

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

    

    NSString *filePath = [documents stringByAppendingPathComponent:@"data.plist"];

    

    //2.创建对象

    dog *superDog = [[dog  alloc]init];

    superDog.name = @"旺财";

    superDog.age = 18;

    superDog.isTrue = YES;

    

    

    //3.归档  NSKeyedArchive

    //如果使用归档 ,所归档的对象 必须遵守NSCoding 协议, 编码协议

    [NSKeyedArchiver archiveRootObject:superDog toFile:filePath];

    

    

    

  

 

    

    

    

    

     NSLog(@"%@",filePath);

}

 

//读取数据

- (IBAction)readDataAction:(UIButton *)sender {

    

    

 

    //1.获取存储的路径

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

    

    NSString *filePath = [documents stringByAppendingPathComponent:@"data.plist"];

    

    

    

    //取出存储的对象

    

  dog *superDog =   [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];

 

    

    NSLog(@"%@今年%@岁了??? === %@",superDog.name,@(superDog.age),@(superDog.isTrue));

    

   

    

 

}

发表评论
用户名: 匿名