Getting an NSAttributedString from NSData/NSTextView using Core Data
Getting an NSAttributedString from NSData/NSTextView using Core Data
- Subject: Getting an NSAttributedString from NSData/NSTextView using Core Data
- From: "Hans Kuder" <email@hidden>
- Date: Mon, 16 Jul 2007 21:44:50 -0400
Hi everyone,
I've got a Core Data app to which I'd like to add searching functionality. I
have a class called MNode, which is a subclass of NSManagedObject. An MNode
stores some of its data in an attribute called nodeData, which is set to
"Binary data" in my object model and is bound to an NSTextView by means of
NSUnarchiveFromData.
Currently this works great - I can save rich text and images in the
NSTextView with no trouble.
To make this data searchable/fetchable, I'd like to keep a plain-text copy
of nodeData as a string attribute in MNode called nodeText. To keep these
two attributes synchronized, MNode has the following method:
- (void)setNodeData:(NSData*)newData
{
NSString *plainText;
[self willChangeValueForKey:@"nodeData"];
[self setPrimitiveValue:newData forKey:@"nodeData"];
[self didChangeValueForKey:@"nodeData"];
NSAttributedString *aString = [[[NSAttributedString alloc]
initWithRTFD:newData documentAttributes:nil] autorelease];
if (aString == nil) {
aString = [[[NSAttributedString alloc] initWithRTF:newData
documentAttributes:nil] autorelease];
}
if (aString == nil) {
NSLog(@"Error getting plain text");
} else {
plainText = [[aString string] copy];
NSLog(@"%@", plainText);
[self willChangeValueForKey:@"nodeText"];
[self setPrimitiveValue:plainText forKey:@"nodeText"];
[self didChangeValueForKey:@"nodeText"];
}
[plainText release];
}
I adapted this from here:
http://www.macgeekery.com/development/searching_rtf_and_rtfd_with_core_data
This method gets called every time nodeData is set, and theoretically should
also set nodeText. Unfortunately, the initWithRTFD:documentAttributes and
initWithRTF:documentAttributes methods of NSAttributedString always return
nil. This happens regardless of whether or not the NSTextView has any
special formatting or images in it.
Any ideas what might be causing the above methods to always return nil? Does
NSTextView store its data in a format different from RTF/RTFD that I should
be checking?
Any help would be greatly appreciated. Thanks!
_______________________________________________
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