Swizzling and keyed archivers

7 views
Skip to first unread message

Andy Balholm

unread,
Jul 3, 2009, 10:47:32 AM7/3/09
to cocotr...@googlegroups.com
I discovered that the Mac version of my program couldn't read documents saved by the Cocotron version. The reason was that objects which were being observed by key-value observing were being archived under their swizzled classes instead of under their real classes.

So here is a patch that changes -class and -classForCoder to return the original class instead of the swizzled one. This seems to be the behavior under MacOS, and it is implied (but not clearly stated) by Apple's documentation for KVO:

When an observer is registered for an attribute of an object the isa pointer of the observed object is modified, pointing to an intermediate class rather than at the true class. As a result the value of the isa pointer does not necessarily reflect the actual class of the instance.

Instead of relying on the isa pointer your application should use the class method to determine the class of an object instance.

This last paragraph implies that isa is the swizzled class, but that class returns the original class.

swizzling.diff

Johannes Fortmann

unread,
Jul 4, 2009, 6:00:47 AM7/4/09
to Cocotron Developers
Since the mailing list drops attachments, I've implemented this
separately. Please have a look at my implementation in r533 and tell
me if this is roughtly equivalent to your solution.

Andy Balholm

unread,
Jul 4, 2009, 10:58:16 AM7/4/09
to cocotr...@googlegroups.com, johannes...@googlemail.com
I think your patch accomplishes the same thing as mine, but mine was simpler.

I only override one method is the swizzled class, -class. I redefined -classForCoder and -className in NSObject in terms of [self class] instead of isa. I'll e-mail you my diff directly.

swizzling.diff
Reply all
Reply to author
Forward
0 new messages