使用NSCoder对NSArray或NSDictionary进行编码 [英] Encode NSArray or NSDictionary using NSCoder

查看:141
本文介绍了使用NSCoder对NSArray或NSDictionary进行编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用NSCoder方法:

I was wondering whether or not it is possible to use the NSCoder method:

- (void)encodeObject:(id)objv forKey:(NSString *)key

编码NSArray或NSDictionary的实例。如果不是,你如何编码呢?我正在尝试使用NSKeyedArchived / NSKeyedUnarchiver使用GameKit在手机之间发送数据。我试过它,似乎无法检索我存储在我的数组中的字符串。我创建的数据包类与数据包类型整数一起出现,但不是数组中的数据。

to encode either an instance of NSArray or NSDictionary. If not how do you go about encoding them? I am trying to use NSKeyedArchived / NSKeyedUnarchiver to send data between phones using GameKit. I tried it and cannot seem to retrieve the string I stored in my array. The packet class I made comes through along with the packet type integer, but not the data in the array it seems.

提前致谢!

推荐答案

NSKeyedArchiver / Unarchiver应该编码和解码NSArrays和NSDictionaries没有问题。如果您创建的数据包类实现了NSCoding协议,则需要显式调用 [encodeObject: myNsArray -encodeWithCoder:方法中 forKey:@stringsArray] (假设 myNsArray 是要编码的数据包对象中的实例变量的名称。但是归档者和NSArray应该照顾其余部分。如果您正在这样做,那么了解更多关于类的布局以及谁在编码/解码时调用谁将会很有帮助。

NSKeyedArchiver/Unarchiver should encode and decode NSArrays and NSDictionaries with no problem. If your packet class you've created implements the NSCoding protocol, you need to explicitly call [encodeObject:myNsArrayforKey:@"stringsArray"] in your -encodeWithCoder: method (assuming that myNsArray is the name of the instance variable in your packet object you want to encode). But then the archiver and NSArray should take care of the rest of it. If you're doing this, it would be helpful to hear more about the layout of your classes and who's calling who when encoding/decoding.

这篇关于使用NSCoder对NSArray或NSDictionary进行编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆