NSMutableArray *array1=[[NSMutableArray alloc]init];
for(int i=0;i<3;i++){
NSNumber *number=[NSNumber numberWithInt:100+i];
[array1 addObject:number];
}
NSMutableArray *array2=[[NSMutableArray alloc]init];
for(int i=0;i<4;i++){
[array2 addObject:[NSString stringWithFormat:@"string%i",i+1]];
}
NSArray * array=@[array1,array2];
BOOL ret=[NSKeyedArchiver archiveRootObject:array toFile:@"/Users/q/Desktop/array.data"];
NSLog(@"ret:%d",ret);
NSArray *array3=[NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/q/Desktop/array.data"];
NSLog(@"array3:%@",array3);
NSMutableDictionary *dict=[NSMutableDictionary dictionary];
[dict setObject:@"zhangsan" forKey:@"username"];
[dict setObject:@"123456" forKey:@"password"];
[dict setObject:[NSDate date] forKey:@"date"];
NSArray * array=@[@"zhangsan@163.com",@"zhangsan@qq.com"];
[dict setObject:array forKey:@"email"];
BOOL ret=[NSKeyedArchiver archiveRootObject:dict toFile:@"/Users/q/Desktop/dict.data"];
NSLog(@"ret=%d",ret);
NSDictionary *dict2=[NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/q/Desktop/dict.data"];
NSLog(@"dict:%@",dict2);
@interface Person : NSObject<NSCoding>
@property(nonatomic,copy)NSString *name;
@property(nonatomic,assign)int ID;
-(NSString *)description
{
return [NSString stringWithFormat:@"name is %@, ID is %d",_name,_ID];
}
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:_name forKey:@"name"];
[aCoder encodeInt:_ID forKey:@"ID"];
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self=[super init];
if(self){
self.name=[aDecoder decodeObjectForKey:@"name"];
self.ID=[aDecoder decodeIntForKey:@"ID"];
}
return self;
}
Person *p=[[Person alloc]init];
p.name=@"zhangsan";
p.ID=1001;
NSString *doc =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [doc stringByAppendingPathComponent:@"asd.archive"];
BOOL ret=[NSKeyedArchiver archiveRootObject:p toFile:path];
NSLog(@"ret:%d",ret);
Person *p2=[NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"p2:%@",p2);
NSString *doc =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [doc stringByAppendingPathComponent:@"asd.plist"];
[responseObject writeToFile:path atomically:YES];
NSArray *array= [NSArray arrayWithContentsOfFile:path];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];