Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old May 14, 2011, 02:32 PM   #1
Sydde
macrumors 68000
 
Sydde's Avatar
 
Join Date: Aug 2009
if ( self == whiner ) B****rds! ;

So I thought, how convenient, I can just save a NSRect using NSValue ó except it was not working, the console said "... this archiver cannot encode structs". Well, dammit, NSValue says it conforms to NSCoding protocol, but when it comes to encoding a NSRect, it chooses not to. Where in the docs does it say that? I looked around but it was not evident anywhere.

Suffice it to say, I simply created my own specific NSRect wrapper object that encodes the elements as NSNumbers, took me all of 20 minutes (because I had to put in every possible method I could think of for the object).

But why can they not at least put a line in somewhere to let a poor, lazy coder know that this will spit up on you?
__________________
You got to be a spirit. You can't be no ghost.
Sydde is offline   0 Reply With Quote
Old May 14, 2011, 05:30 PM   #2
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
I guess you missed NSStringFromRect() and NSRectFromString()

And the reason it probably doesn't archive structs is because archives work on all architectures and dumping raw structs to file would fail to work for all cases across 32/64-bit due to padding.
kainjow is offline   0 Reply With Quote
Old May 14, 2011, 05:49 PM   #3
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
Hmmm... NSValue's valueWithRect: didn't work? What about NSCoder's encodeRect: or encodeRect:forKey:? Which archiver are trying to use?
jiminaus is offline   0 Reply With Quote
Old May 14, 2011, 06:04 PM   #4
Sydde
Thread Starter
macrumors 68000
 
Sydde's Avatar
 
Join Date: Aug 2009
Quote:
Originally Posted by kainjow View Post
I guess you missed NSStringFromRect() and NSRectFromString()
What, use functions? zomg, this is Objective-C, I am supposed to use objects.
Quote:
And the reason it probably doesn't archive structs is because archives work on all architectures and dumping raw structs to file would fail to work for all cases across 32/64-bit due to padding.
Yes, I do understand the issues with just flat laying out bytes and hoping that they will never be used on a different type of machine. But I figured NSValue might at least be smart enough to know what it contains and encode appropriately. Or maybe that Apple would have the courtesy to let me know that this was going to spit up on me. Just one little sentence in the overview that tells you NSValue might not be able to encode non-primitives.
__________________
You got to be a spirit. You can't be no ghost.
Sydde is offline   0 Reply With Quote
Old May 14, 2011, 06:11 PM   #5
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
Quote:
Originally Posted by Sydde View Post
Or maybe that Apple would have the courtesy to let me know that this was going to spit up on me. Just one little sentence in the overview that tells you NSValue might not be able to encode non-primitives.
But it's not NSValue's fault. The message says the archiver cannot encode structs. The archiver is the NSCoder object, not the NSCoding object. The fault is with the NSCoder object being used. And both NSValue and NSCoder both explicitly support rects. So what are you doing wrong?
jiminaus is offline   0 Reply With Quote
Old May 14, 2011, 06:50 PM   #6
Sydde
Thread Starter
macrumors 68000
 
Sydde's Avatar
 
Join Date: Aug 2009
Quote:
Originally Posted by jiminaus View Post
But it's not NSValue's fault. The message says the archiver cannot encode structs. The archiver is the NSCoder object, not the NSCoding object. The fault is with the NSCoder object being used. And both NSValue and NSCoder both explicitly support rects. So what are you doing wrong?
No, actually, if you look at the docs for NSCoder:

Quote:
encodeRect:
Encodes rect.

- (void)encodeRect: (NSRect)rect

Discussion
NSCoderís implementation invokes encodeValueOfObjCType:at: to encode rect.
.
.
.
encodeValueOfObjCType:at:
Must be overridden by subclasses to encode a single value residing at address, whose Objective-C type is given by valueType.

- (void)encodeValueOfObjCType: (const char *)valueType at: (const void *)address
Naturally, I am using NSKeyedArchiver, which is a subclass of NSCoder. NSCoder is an abstract class, meaning any methods it actually implements are placeholders or exception throwers. Apparently, NSKeyedArchiver does not support archiving NSRect structs, probably for the reasons kainjow put forth. Perhaps it did in 10.3 or 10.2, when the architecture was homogenous (all 32 bit BE), but now it does not.
__________________
You got to be a spirit. You can't be no ghost.
Sydde is offline   0 Reply With Quote
Old May 14, 2011, 10:10 PM   #7
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
Quote:
Originally Posted by Sydde View Post
No, actually, if you look at the docs for NSCoder.
Slap, ow! But rightly deserved.


Quote:
Originally Posted by Sydde View Post
NSCoder is an abstract class, meaning any methods it actually implements are placeholders or exception throwers.
It's not necessarily a pure abstract class. Some of the methods could be implemented in terms of a subset of the API that is actually abstract. For example, encodeRect:forKey: could have been implemented in terms of encodePoint:forKey: and encodeSize:forKey:. These in turn could have been implemented in terms of either encodeFloat:forKey: or encodeDouble:forKey:.

But now I completely agree with your rant. How hard would it have been for Apple to implement encodeRect:forKey: and decodeRect:forKey: using NSStringForRect and NSRectForString respectively. These wouldn't have had any platform dependencies because the numbers would have been written and read as text not binary.
jiminaus is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
(NSError **) error ; Why two pointers (*)? Extreme343GS Mac Programming 6 Aug 17, 2011 01:28 AM
bad headphone ( audio out ) on iMac ( White) alecmcmahon iMac 0 Jul 7, 2008 05:08 PM
new to mac ( bought ibook ) gizmo_pony PowerPC Macs 24 Aug 12, 2005 10:14 AM
USB - SCSI Converter with scanner ( Entrega / Xircom ) butterball Macintosh Computers 0 Mar 7, 2005 06:36 AM
PC sees Mac share but Mac doesn't see PC share ( well sometimes ) fiona_mac General Mac Discussion 0 Jan 11, 2005 02:22 PM


All times are GMT -5. The time now is 02:08 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC