gpt4 book ai didi

iphone - 将 NSObject 转换为 NSData

转载 作者:行者123 更新时间:2023-12-03 18:23:05 24 4
gpt4 key购买 nike

我在将 NSObject 转换为 NSData 时遇到问题。我有一个继承 NSObject 的类。

当我尝试将该特定类的对象转换为 NSData 时,如下所示:

NSData *dataOnObject = [NSKeyedArchiver archivedDataWithRootObject:classObject];

但它给出了异常,指出 -[classObjectencodeWithCoder:]: 无法识别的选择器发送到实例..

我还将对象添加到新创建的数组中,如下所示

NSMutableArray *wrapperedData = [NSMutableArray arrayWithObject: classObject];
NSData *dataOnObject = [NSKeyedArchiver archivedDataWithRootObject:value];

但是,它仍然给出了异常。

所以我需要从对象classObject中提取字节。

任何帮助将不胜感激......

等待您的回复...

最佳答案

您必须实现自己的对象,例如:

- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInt:self.age forKey:@"age"];
[aCoder encodeObject:self.email forKey:@"email"];
[aCoder encodeObject:self.password forKey:@"password"];
}
BOOL success = [NSKeyedArchiver archiveRootObject:person toFile:archiveFilePath];

和:

- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super init]) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeIntForKey:@"age"];
self.email = [aDecoder decodeObjectForKey:@"email"];
self.password = [aDecoder decodeObjectForKey:@"password"];
}
return self;
}
Person *unarchivePerson = [NSKeyedUnarchiver unarchiveObjectWithFile:archiveFilePath];

关于iphone - 将 NSObject 转换为 NSData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1659922/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com