编码时 NSArray 对象会发生什么?

发布于 2024-12-13 12:47:36 字数 1400 浏览 0 评论 0原文

我正在构建一个利用 NSCoding 将 NSObject 保存到 documentPath 的应用程序。 我这样做没有任何问题,我只是对某些事情感到好奇。

我有 MACompany,它实现了 NSCoding 委托方法。

- (void) encodeWithCoder:(NSCoder *)encoder {

     [encoder encodeObject:address_1 forKey:kAddress_1];
     [encoder encodeObject:address_2 forKey:kAddress_2];
     [encoder encodeObject:city_town forKey:kCity_Town];
     [encoder encodeObject:company_name forKey:kCompany_Name];
     [encoder encodeObject:country forKey:kCountry];
     [encoder encodeObject:date_added forKey:kDate_Added];
     [encoder encodeObject:fax forKey:kFax];
     [encoder encodeObject:parent_company_website forKey:kWebsite];
     [encoder encodeObject:postal_code forKey:kPostal_Code];
     [encoder encodeObject:state_province forKey:kState_Province];
     [encoder encodeObject:type forKey:kType];

     [encoder encodeObject:stores forKey:kStores]; //NSArray of custom NSObjects

}

如您所见,我有一个自定义 NSObject 的 NSArray (MAStore)。这些对象中的每一个都实现了相同的 NSCoding,但没有实现相同的 NSCoding。

但是,我的问题是,当我在 MAStore 中调用 encodeWithCoder:(NSCoder *)encoder 方法,并且它到达 [encoderencodeObject:stores forKey:kStores] 时,将存储在 stores NSArray 中的所有对象都调用了 encoderWithCoder:(NSCoder *)encoder 方法(如果实现的话)?

编辑

我问这个问题的原因是我想在投入时间做这样的事情之前知道它是否有效。我有多个自定义 NSObject,其中 NSArray 包含更多自定义 NSObject。发现它不起作用将是一个漫长的过程。

I'm building an application that utilises NSCoding to save NSObject's to a documentPath.
I'm having no issues doing this, I'm just curious about something.

I have MACompany, which implements NSCoding delegate methods.

- (void) encodeWithCoder:(NSCoder *)encoder {

     [encoder encodeObject:address_1 forKey:kAddress_1];
     [encoder encodeObject:address_2 forKey:kAddress_2];
     [encoder encodeObject:city_town forKey:kCity_Town];
     [encoder encodeObject:company_name forKey:kCompany_Name];
     [encoder encodeObject:country forKey:kCountry];
     [encoder encodeObject:date_added forKey:kDate_Added];
     [encoder encodeObject:fax forKey:kFax];
     [encoder encodeObject:parent_company_website forKey:kWebsite];
     [encoder encodeObject:postal_code forKey:kPostal_Code];
     [encoder encodeObject:state_province forKey:kState_Province];
     [encoder encodeObject:type forKey:kType];

     [encoder encodeObject:stores forKey:kStores]; //NSArray of custom NSObjects

}

As you can see, I have a NSArray of custom NSObjects (MAStore). Each one of these objects also implements the same NSCoding what not.

However, my question is, when I call encodeWithCoder:(NSCoder *)encoder method in MAStore, and it gets to the [encoder encodeObject:stores forKey:kStores], will all the objects stored within the stores NSArray have the encoderWithCoder:(NSCoder *)encoder method called if implemented?

EDIT

The reason I'm asking this is that I want to know whether or not it'll work before I invest time in doing such a thing. I have multiple custom NSObjects with NSArrays that hold more custom NSObjects. It would be a long process to find that it doesn't work.

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

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

发布评论

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

评论(1

月朦胧 2024-12-20 12:47:36

是的。编码器递归地进入容器对象(数组、字典、集合等)并对所有这些对象进行编码。如果内部对象之一不支持 NSCoding,您将遇到异常。

Yes. The encoder recursively walks into container objects (arrays, dictionaries, sets, etc) and encodes all of those objects as well. You'll hit an exception if one of the inner objects doesn't support NSCoding.

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