NSCoder - 使用多层嵌套数组对数组进行编码

发布于 2025-01-07 02:05:10 字数 368 浏览 3 评论 0原文

我有一个 mainObjectArray (NSMutableArray),其中填充了自定义类的实例。每个实例本身就是一个数组,每个数组中的对象是 NSDates、NSStrings、BOOL 以及更多包含类似对象的数组。

我无法确定的是,是否可以在

- (void)encodeWithCoder:(NSCoder *)encoder 

方法内部简单地说这样的话:

[encoder encodeWithObject:mainObjectArray];

或者是否必须分别对每个实例中的每个对象进行编码?这会有点痛苦......

我们将非常感谢您的帮助。

I have a mainObjectArray (NSMutableArray) which is populated with instances of a custom class. Each instance is itself an array, and objects in each array are NSDates, NSStrings, BOOL, and more arrays containing similar objects.

What I haven't been able to establish is whether it's possible to, inside the

- (void)encodeWithCoder:(NSCoder *)encoder 

method, to just say something like that:

[encoder encodeWithObject:mainObjectArray];

Or do have to encode every object in every instance separately? This would be a bit of a pain...

Your help would be very much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花心好男孩 2025-01-14 02:05:10

只需在您的自定义类中实现编码和解码方法即可。这样就可以了。一些示例,

- (void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeObject:[NSNumber numberWithInt:pageNumber] forKey:@"pageNumber"];
    [encoder encodeObject:path forKey:@"path"];
    [encoder encodeObject:array forKey:@"array"];
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if(self = [super init]) 
    {
        self.pageNumber = [[aDecoder decodeObjectForKey:@"pageNumber"] intValue];
        self.path = [aDecoder decodeObjectForKey:@"path"];
        self.array = [aDecoder decodeObjectForKey:@"array"];
    }
}

您可以看到总共三种数据类型被编码和解码 - int、string、array。

希望这有帮助。

Just implement the encoding and decoding methods in your custom class. That will do. Some sample,

- (void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeObject:[NSNumber numberWithInt:pageNumber] forKey:@"pageNumber"];
    [encoder encodeObject:path forKey:@"path"];
    [encoder encodeObject:array forKey:@"array"];
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if(self = [super init]) 
    {
        self.pageNumber = [[aDecoder decodeObjectForKey:@"pageNumber"] intValue];
        self.path = [aDecoder decodeObjectForKey:@"path"];
        self.array = [aDecoder decodeObjectForKey:@"array"];
    }
}

You can see totally three data types being encoded and decoded - int, string, array.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文