您好,登錄后才能下訂單哦!
NSKeyedArchiver是一個用于將對象序列化為NSData格式的類,我們可以通過實現NSCoding協議來自定義對象的序列化過程。
@interface CustomObject : NSObject <NSCoding>
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger age;
@end
@implementation CustomObject
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInteger:self.age forKey:@"age"];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
if (self) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeIntegerForKey:@"age"];
}
return self;
}
@end
CustomObject *object = [[CustomObject alloc] init];
object.name = @"John";
object.age = 30;
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:object];
CustomObject *decodedObject = [NSKeyedUnarchiver unarchiveObjectWithData:data];
這樣,我們就可以通過NSKeyedArchiver自定義對象的序列化過程,將對象以NSData的形式存儲或傳輸。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。