NSValueTransformer: reverse transforms like NSUnarchiveFromData
NSValueTransformer: reverse transforms like NSUnarchiveFromData
- Subject: NSValueTransformer: reverse transforms like NSUnarchiveFromData
- From: Rams <email@hidden>
- Date: Wed, 5 Jan 2005 21:16:19 -0500
Could someone explain to me how a NSValueTransformer like
NSUnarchiveFromData works? I'm trying to do something similar in the
respect that I need to transform one type of object (NSString) into
another (NSArray) and back again. Specifically,
reverseTransformedValue: is never getting called.
My setup: I have an NSObjectController that contains a value that must
be stored as a string. My UI contains a NSTableView that is bound to a
NSArrayController. The values contained by this array controller need
to be stored in that string. NSValueTransformer to the rescue? Not
yet. I've tried creating a transformer to put between the two
controllers and only the transformedValue: method gets called. I've
tried an 'else if' branch to look for and send the array to the
reverseTransformedValue: method. More failure. My question then is
two-fold. Why doesn't the reverseTransformedValue method ever get
called? Why does my workaround fail with a:
*** -[NSCFString count]: selector not recognized [self = 0x1390d30]
I've actually reached the point where I'm basically convinced that a
simple text field would be just as useable in the UI anyway, but the
answer to these questions are still nagging me. I am now more curious
as to why it didn't work, whether or not I actually implement the UI
that way. I'm a bit of a Obj-C noob but familiar with Cocoa/Java, so
maybe my code stinks. Here's the interface and implementation:
#import <Cocoa/Cocoa.h>
@interface UnarchiveFromString : NSValueTransformer {
NSString *delimiter;
NSString *dictKey;
}
@end
#import "UnarchiveFromString.h"
@implementation UnarchiveFromString
+ (Class)transformedValueClass; {
return [NSArray self];
}
+ (BOOL)allowReverseTransformation; {
return YES;
}
- (id)init {
if (self = [super init]) {
/* class-specific initialization goes here */
// Set magic strings.
delimiter = @" ";
[delimiter retain];
dictKey = @"group";
[dictKey retain];
}
return self;
}
- (id)transformedValue:(id)value {
NSLog(@"transformedValue entered");
//Transform from string to an array of dictionaries here
if ([value isKindOfClass: [NSString class]]) {
NSLog(@"Transforming string to array");
NSArray *valueArray = [value
componentsSeparatedByString:delimiter];
NSMutableArray *dictArray = [NSMutableArray
arrayWithCapacity:[valueArray count]];
id item;
NSEnumerator *objectsEnumerator = [valueArray objectEnumerator];
while (item = [objectsEnumerator nextObject]) {
[dictArray addObject:[NSMutableDictionary
dictionaryWithObject: item forKey: dictKey]];
}
return dictArray;
} else if ([value isKindOfClass: [NSArray class]]) { //Ugly broken
hack.
return [self reverseTransformedValue: value];
}
return nil; //To hush the compiler warning.
}
- (id)reverseTransformedValue:(id)value {
NSLog(@"reverseTransformedValue entered");
//Transform from array of dictionaries to string here
if ([value isKindOfClass: [NSArray class]]) {
NSLog(@"Transforming array to string");
return [[value valueForKey: dictKey]
componentsJoinedByString:delimiter];
}
return nil;
}
@end
Thanks! Any advice will be appreciated.
--
Learn how to cryptographically sign your mail in Panther
http://www.joar.com/certificates/
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden