Re: How to store NSRect as Core Data attribute?
Re: How to store NSRect as Core Data attribute?
- Subject: Re: How to store NSRect as Core Data attribute?
- From: Adam P Jenkins <email@hidden>
- Date: Wed, 9 Apr 2008 08:43:45 -0400
Hmm, seems I was misunderstanding what the built-in value transformers
do, and also had a problem with my custom transformer. The
NSUnarchiveFromData transformer won't work for this purpose. However
you can make it possible to store NSValue objects as Core Data
attributes by using a Transformable attribute, and writing your own
ValueTransformer. Here's a trivial value transformer that I just
tried, which allows storing any NSCoding compliant object as an
attribute value, including NSValues containing an NSRect. To use it,
add this class to your project, then create a Core Data attribute of
type Transformable, and set its Value Transformer Name to
MyValueTransformer.
// MyValueTransformer.h
#import <Cocoa/Cocoa.h>
@interface MyValueTransformer : NSValueTransformer {
}
@end
// MyValueTransformer.m
#import "MyValueTransformer.h"
@implementation MyValueTransformer
+ (Class)transformedValueClass
{
return [NSData class];
}
+ (BOOL)allowsReverseTransformation
{
return YES;
}
- (id)transformedValue:(id)value
{
return [NSArchiver archivedDataWithRootObject:value];
}
// input is expected to be an NSData object
- (id)reverseTransformedValue:(id)data
{
return [NSUnarchiver unarchiveObjectWithData:data];
}
@end
On Apr 9, 2008, at 1:16 AM, Adam P Jenkins wrote:
Actually on Leopard you should be able to store an NSValue as an
attribute, simply by using an attribute of type Transformable, and
setting the Value Transformer Name to NSUnarchiveFromData.
I just tried it though, and it seems like there is a bug which
prevents it from working. Basically it seems like any custom value
transformer you set for a Transformable attribute is simply ignored,
and it still just uses the default transformer, which tries to use a
KeyedArchiver and KeyedUnarchiver. I even tried creating my own
NSValueTransformer subclass and setting it as the value transformer
for a Transformable attribute. I can see that its init method gets
called, but none of its other methods ever get called when saving a
document. I just filed a bug with Apple about this, radar 5851442.
On Apr 3, 2008, at 5:31 AM, Daniel Thorpe wrote:
Thanks for the feedback on this.
I have gone with using a ...AsString attribute and using
NSRectFromString. Seems to work okay, although I've got no idea if
it's the most efficient method. I think Core Data seems a little
limited in that you can't store an NSValue object as an attribute,
I'd have thought that would be obvious, as we use that class to
store structs in collection objects....
I hadn't considered localisation issues, but on my machine it
stores the data like so in an XML store:
<attribute name="extentasstring" type="string">{{368, 260},
{2, 5}}</attribute>
<attribute name="centreofmassasstring" type="string">{368.75,
263.25}</attribute>
which seems to make sense.
Cheers
Dan
On 2 Apr 2008, at 18:33, Uli Kusterer wrote:
On 02.04.2008, at 18:58, Thomas Engelmeier wrote:
That potentially means opening Pandoras can to localisation
issues. Any time you go that route, be sure to check that code
with switching decimal separators.
Potentially? Maybe. But I just tried it, and for me
NSStringFromRect() never uses localized separators. So, since it
always gives and takes periods as the decimal separator, I see no
problem. Do you have a particular test case where it behaves
differently?
Cheers,
-- Uli Kusterer
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de
_______________________________________________
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
_______________________________________________
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:
@thejenkins.org
This email sent to email@hidden
_______________________________________________
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