iOS NData分类--可序列化

对实现了NSSecureCoding协议的类进行数据本地化

@implementation NSData (AlivcHelper)

+ (NSData *)dataWithObject:(id)object{
    NSMutableData *data = [[NSMutableData alloc]init];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
    [archiver setRequiresSecureCoding:YES];
    [archiver encodeObject:object forKey:NSKeyedArchiveRootObjectKey];
    [archiver finishEncoding];
    return data;
}

+ (nullable id)customInstanceFromData:(NSData *)data forClassType:(Class)classType {
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
    [unarchiver setRequiresSecureCoding:YES];
    id object = [unarchiver decodeObjectOfClass:[classType class] forKey:NSKeyedArchiveRootObjectKey];
    return object;
}

你可能感兴趣的:(iOS NData分类--可序列化)