Re: NSTextView binding with CoreData
Re: NSTextView binding with CoreData
- Subject: Re: NSTextView binding with CoreData
- From: Michael Fey <email@hidden>
- Date: Tue, 31 Jul 2007 08:13:23 -0400
Nathan,
We hit the same exact problem in our app. Here's how we fixed it:
While the NSTextView will not accept NSString data, it will accept
NSAttributedString data. Armed with that knowledge we wrote a
"StringToAttribStringTransformer" that is a subclass of
NSValueTransformer. Following the documentation from
NSValueTransformer we implemented the following three methods:
+ (Class)transformedValueClass
{
return [NSAttributedString class];
}
+ (BOOL)allowsReverseTransformation
{
return YES;
}
/*
* Transform NSString to NSAttributedString
*
*/
- (id)transformedValue:(id)value
{
if(value == nil)
return nil;
NSAttributedString* attribString = [[[NSAttributedString alloc]
initWithString:(NSString*)value] autorelease];
return attribString;
}
/*
* Transform NSAttributedString to NSString
*
*/
- (id)reverseTransformedValue:(id)value
{
if(value==nil)
return nil;
return [(NSAttributedString*)value string];
}
The next step was to register the value transformer so that our app
knew to use it when asked for it. In the init method we first
declare an instance of our new value transformer:
StringToAttribStringTransformer* stringToAttribStringTransformer =
[[[StringToAttribStringTransformer alloc] init] autorelease];
And then we call the NSValueTransformer's static
setValueTransformer:forName: method:
[NSValueTransformer
setValueTransformer:stringToAttribStringTransformer
forName:@"StringToAttribStringTransformer"];
The last step in getting the NSTextView to properly display data from
your NSArrayController is to tell the binding which value transformer
to use. Open IB, select the NSTextView, go to the Value ->
attributedString section of the bindings panel and bind it to your
ArrayController's Controller Key and Model Key Path. Lastly, type
"StringToAttribStringTransformer" into the Value Transformer field.
That should be all you need to do, but if I missed a step don't
hesitate to let me know.
Regards,
Michael Fey
email@hidden
On Jul 30, 2007, at 5:37 PM, mmalc crawford wrote:
On Jul 30, 2007, at 2:24 PM, Nathan Gilmore wrote:
The binding for the NSTextView looks like this:
data
Bind To: Notes (NSArrayController)
Controller Key: Selection
Model Key Path: body
However I am getting this error:
2007-07-30 17:07:13.907 xNote4[3928] Unacceptable type of value
for attribute: property = "body"; desired type = NSString; given
type = NSConcreteMutableData;
Sometimes looking carefully at the error message is useful.
Exactly as the error message states, you're not sending a string.
See <http://developer.apple.com/documentation/Cocoa/Reference/
CocoaBindingsRef/BindingsText/NSTextView.html>L
Contrast 'data' and 'value' bindings.
mmalc
_______________________________________________
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:
40partech.com
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