今天上午讲的内容是,关于归档和解归档的问题
标准分辨归档的格式是:
NSString *str = @"abc";
[NSKeyedArchiver archiveRootObject:str toFile:@"/Users/apple/Desktop/test.plist"];
//将字符串str写入test.plist文件,归档
标准分辨解归档的格式是:
NSString *str1 = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/apple/Desktop/test.plist];
//将test.plist文件的内容读取到字符串str1,解档
两句对比:
[NSKeyedArchiver archiveRootObject:str
toFile:@"/Users/apple/Desktop/test.plist"];
[NSKeyedUnarchiver unarchiveObjectWithFile:
@"/Users/apple/Desktop/test.plist];
关键词: archiver 与 unarchiver
归档与解归档作用的对象,可以是不一样的,珊哥今天的讲解的方式是,通过不断的归档与解归档来比较,两个不同的对象在进行归档与解归档的时候它的值的变化。
除了归档与解归档以外,其他方面也是有所接触,例如在进行归档与解归档的时候,引用的协议相关动作的步骤:
--> <NSCoding>
--> command+单击
--> - (void)encodeWithCoder:(NSCoder *)aCoder;
- (id)initWithCoder:(NSCoder *)aDecoder;
这两个方式是在引用 NSCoding 协议以后必须要完成的:
--> - (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:_name forKey:kNameKey];
[aCoder encodeInteger:_age forKey:kAgeKey];
[aCoder encodeObject:_dog forKey:kDogKey];
//
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
self.name = [aDecoder decodeObjectForKey:kNameKey];
self.age = [aDecoder decodeIntegerForKey:kAgeKey];
self.dog = [aDecoder decodeObjectForKey:kDogKey];
}
return self;
ps:讲到这里我发现我犯了一个错:
self.age = [aDecoder decodeIntegerForKey:kAgeKey];
这一句我也成了:
self.name = [aDecoder decodeObjectForKey:kAgeKey];
写代码的时候,我时常会犯一些错,然后使劲看代码找错误,现在学着珊哥说的那样,先看错误提示找准哪里错了,再改。可是看不懂,错误提示~真坑,争取以后错了找个本子记下来吧,这样的话虽然很不方便,也不失为一个办法吧!只要是按我想的发展,按道理来说应该是有用的!
此外,必须还需要进行宏定义三个变量:
#define kNameKey @"name"
#define kAgeKey @"age"
#define kDogKey @"dog"
以上操作是新建Student.m文件中进行
另外在 新建Student.h 文件中
对 name 、 age 、 dog 三个属性进行定义:
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger age;
@property (nonatomic, strong) Dog *dog;
对于归档与解归档的代码验证,如下:
ViewController.h
->
#import <UIKit/UIKit.h>
#import "Student.h"
@interface ViewController : UIViewController
@property (nonatomic,strong) Student *student;
@end
<-
ViewController.m
->
#import "ViewController.h"
#import "Student.h"
#import "Dog.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *str = @"abc";
[NSKeyedArchiver archiveRootObject:str toFile:@"/Users/apple/Desktop/test.plist"];
//将字符串str写入test.plist文件,归档
NSString *str1 = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/apple/Desktop/test.plist"];
//将test.plist的文件内容读取到字符串str1,解档
NSLog(@"%@",str1);
Student *stu = [[Student alloc]init];
stu.name = @"Zhangsan";
stu.age = 30;
[NSKeyedArchiver archiveRootObject:stu toFile:@"/Users/apple/Desktop/student.plist"];
Student *stu1 = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/apple/Desktop/student.plist"];
// 上面两条语句的意思是将 stu 的值存档,地址也给出了。再由 stu1 进行读档,即将文档的内容读取出来,并且赋值给 stu1.
NSLog(@"%@,%@",stu.name,stu1.name);
NSLog(@"___%d,%d",stu.age,stu1.age);
NSLog(@"%@:%p %@:%p",str,stu,str1,stu1);
NSData *stuData = [NSKeyedArchiver archivedDataWithRootObject:stu];
[stuData writeToFile:@"/Users/apple/Desktop/stu.plist" atomically:YES];
NSData *stuData2 = [NSData dataWithContentsOfFile:@"/Users/apple/Desktop/stu.plist"];
// NSLog(@"~~~%d",stuData2);
Student *stu3 = [NSKeyedUnarchiver unarchiveObjectWithData:stuData2];
NSLog(@"%@:%d----%@:%d",stu.name,stu.age,stu3.name,stu3.age);
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:stuData forKey:@"xxxx"];
NSData *stuData3 = [userDefaults objectForKey:@"xxxx"];
Student *stu4 = [NSKeyedUnarchiver unarchiveObjectWithData:stuData3];
NSLog(@"%@:%d",stu4.name,stu4.age);
stu4.dog = [[Dog alloc]init];
stu4.dog.name = @"小强";
[NSKeyedArchiver archiveRootObject:stu4 toFile:@"/Users/apple/Desktop/dog.plist"];
NSLog(@"%@",stu4.dog.name);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
<-
Student.h
->
#import <Foundation/Foundation.h>
#import "Dog.h"
@interface Student : NSObject <NSCoding>
@property (nonatomic,assign) NSInteger age;
@property (nonatomic,copy) NSString *name;
@property (nonatomic,strong) Dog *dog;
@end
<-
Student.m
->
#import "Student.h"
#define kAgeKey @"age"
#define kNameKey @"name"
#define kDogKey @"dog"
@implementation Student
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeInt:_age forKey:@"age"];
[aCoder encodeObject:_name forKey:@"name"];
[aCoder encodeObject:_dog forKey:@"dog"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
self.age = [aDecoder decodeIntegerForKey:kAgeKey];
self.name = [aDecoder decodeObjectForKey:kNameKey];
self.dog = [aDecoder decodeObjectForKey:kDogKey];
return self;
}
@end
<-
Dog.h
->
#import <Foundation/Foundation.h>
@interface Dog : NSObject <NSCoding>
@property (nonatomic,copy)NSString *name;
@end
<-
Dog.m
->
#import "Dog.h"
#define kDogNameKey @"name"
@implementation Dog
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:_name forKey:kDogNameKey];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
self.name = [aDecoder decodeObjectForKey:kDogNameKey];
}
return self;
}
@end
<-
归档与解归档是啥 你懂么~
最新推荐文章于 2022-10-01 16:24:24 发布