NSCoder blues (was Re: How to archive structs like NSRect)
NSCoder blues (was Re: How to archive structs like NSRect)
- Subject: NSCoder blues (was Re: How to archive structs like NSRect)
- From: David Spooner <email@hidden>
- Date: Mon, 12 Nov 2007 12:55:02 -0700
I would like to take this opportunity to (respectfully) express my
concerns about the apparent rift in the NSCoder classes.
It would seem that NSArchiver is being deprecated since newer classes
such as NSIndexPath do not implement non-keyed coding and (as Doug
Knowles pointed out) NSPredicate no longer supports non-keyed coding
in 10.5. I think NSKeyedArchiver was a significant step forward in
dealing with legacy coding issues, but there are two significant
problems :
1) NSPortCoder does not support keyed coding
2) NSKeyedArchiver does not provide the same level of support for
non-object types as NSArchiver does
I suspect the problem which Gordon has observed with NSValue is a
consequence of 2, since presumably NSValue's -encodeWithCoder: uses -
encodeValueOfObjCType:at:.
As another example of why 2 is a problem, suppose I have a triangle
mesh object with vertices represented as a c-array of struct {float x,
y, z;}. With an NSArchiver I could encode the vertex array using
either -encodeArrayOfObjCType:count:at: or -
encodeValueOfObjCType:at:. An NSKeyedArchiver throws an exception
when using the non-keyed methods in this situation (although it does
work for simpler c-types such as an array of floats).
I realize that I could encode the vertices as NSData, but in order to
be architecture independent I would have to first use NSArchiver to
create the NSData and then encode the data using -encodeObject:forKey:
-- an arguably unnecessary copy. It would be far preferable to have a
keyed-coding method -encodeValueOfObjCType:forKey:at: which provides
the functionality which I have come to expect from NSArchiver.
I have filed an enhancement request (5163604) to this effect...
Cheers,
dave
On 12-Nov-07, at 11:16 AM, Gordon Apple wrote:
That doesn't work. NSKeyedArchiver chokes on it. That's
currently high
on my gripe list. I resorted to class-testing and wrapping NSValues
in
NSData, although I may change to the string approach for rects. I am
thankful that you can now at least archive NSColor with a keyed
archiver. I
wish they would include NSValues. IMHO, if you can put it into an
array or
a dictionary, you should be able to archive it.
BTW, if you take my approach of class-testing be sure and use
"isKindOf", not "isMemberOf", because NSValue is not just a class --
it is a
class cluster. (Found this out the hard way).
Message: 3
Date: Mon, 12 Nov 2007 09:27:33 -0500
From: Hank Heijink <email@hidden>
Subject: Re: How to archive structs like NSRect
To: Andrew Ebling <email@hidden>
Cc: "email@hidden" <email@hidden>
Message-ID: <email@hidden>
Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes
You can wrap an NSRect in an NSValue, but I just use NSRectToString
and NSStringToRect to make it an object.
Hope that helps,
Hank
On Nov 12, 2007, at 9:12 AM, Andrew Ebling wrote:
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden