通用 NSCoding 实现的 Objective-C Reflection
Objective-C 中是否有任何反射方法允许您通过检查对象的公共属性并生成 encodeWithCoder: 和 initWithCoder: 的通用实现来编写通用 NSCoding 实现。
我正在考虑类似 XStream for Java 的东西,它允许使用通用方法来序列化和反序列化 Java使用反射的对象。 更好的方法可能是将属性标记为您想要序列化的东西或瞬态的东西(如 Java 中的瞬态关键字)。
我一直在阅读 档案 和Cocoa 序列化编程指南。 我知道您希望对对象的序列化进行一些控制,但这通常是一个对称的过程,并且必须反转序列化编码以反序列化它似乎很奇怪。 我是 DRY 的信徒(不要重复自己)。
Is there any means of reflection in Objective-C that would allow you to write generic NSCoding implementations by inspecting the public properties of an object and generating generic implementations of encodeWithCoder: and initWithCoder: .
I'm thinking of something like XStream for Java that allows a generic way to serialize and deserialize Java objects using reflection. Even better would probably be some means of marking properties as things you'd want to serialize or that are transient (like the transient keyword in Java).
I've been reading the documentation on Archives and Serializations Programming Guide for Cocoa. I understand that you want some control over the serialization of your objects, but it is generally a symmetrical process and it seems odd to have to reverse what is coded for serialization to deserialize it. I'm a believer of DRY (don't repeat yourself).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这不仅是可能的,而且我有一个朋友正在尝试这样做。 (您可以在此处查看他的有关此内容的博客.) 反射是使用 Objective-C 2.0 运行时参考。 看一看。
但请注意,只有当您想要保存所有实例变量的通用行为时,这才有效。 不过,您可能不希望 NSView 保存其超级视图; 在这种情况下,通用情况将不起作用。
您可以通过为要保存的任何实例变量声明属性并保留任何其他变量“隐藏”来区分要序列化的事物和不可序列化的事物,但这将属性的整个目的扭曲到一个小小的好处。 我不会推荐它。
Not only is it possible, but I have a friend who's taken a stab at doing precisely that. (You can see his blog about it here.) The reflection is done using the Objective-C runtime functions documented in the Objective-C 2.0 Runtime Reference. Take a look.
Note, however, that this will only work if you want the generic behavior of saving all the instance variables. You might not want an NSView to save its superview, though; in such cases, the generic case wouldn't work.
You could conceivably distinguish between things-to-serialize and things-not-to-serialize by declaring properties for any instance variables you want to save and leaving any other variables "hidden", but that's twisting the whole purpose of properties to a small benefit. I wouldn't recommend it.
(我是 BJ Homer 评论中链接的博客文章的作者)。 使用代码有几个注意事项:
我写这篇文章主要是为了概念证明,在很多情况下,这段代码会严重失败。 例如,在对象 A 中,B 有一个 ivar,而 B 有 A 的 ivar,然后使用代码序列化其中一个或另一个(我相信)会导致无限循环。
尽管如此,我认为 Objective-C 甚至允许你做像一开始那样的事情,这真是太棒了。
(I am the author of the blog post linked to in BJ Homer's comment). There are a couple caveats to using the code:
I wrote this mainly as a proof of concept, and there are many instances when this code would fail spectacularly. For example, in object A has an ivar for B, and B has an ivar for A, then using the code to serialize one or the other would (I believe) cause an infinite loop.
Nevertheless, I think it's pretty freaking awesome that Objective-C even allows you to do stuff like in the first place.
查看 RMModelObject:
http://www.realmacforge.com/svn/trunk/RMModelObject/< /a>
但是,请注意 ObjC 运行时功能可以在 10.5 和模拟器中运行,但不能在实际的 iPhone 上运行。 艰难地发现了这一点..也许OS 3.0终于支持它了,但我还没有检查过。
Check out RMModelObject:
http://www.realmacforge.com/svn/trunk/RMModelObject/
However, note that ObjC Runtime stuff works in 10.5, and in the Simulator, but not on an actual iPhone. Found that out the hard way.. Maybe OS 3.0 finally supports it, but I haven't checked.
今晚就是为了这个目的写这篇文章的; 不要忘记#import! 这假设类中的所有 @properties 都是 NSObject 形式,因此 NSNumber 而不是 float 等。
Wrote this tonight for this very purpose; don't forget the #import! This assumes all @properties in your class are NSObject-form, so NSNumber instead of float, etc.