title: Transform String to Core Data by NSValueTransformer
body: <p>I'm trying to use Core Data <code>NSValueTransformer</code> to transform <code>NSArray</code> to <code>NSString</code>. I'm not sure whether it can be archived, but I saw apple's official doc show a code snippet:</p>
  
  <pre><code>@interface ClassNameTransformer: NSValueTransformer {}
  @end
  @implementation ClassNameTransformer
  + (Class)transformedValueClass { return [NSString class]; }
  + (BOOL)allowsReverseTransformation { return NO; }
  - (id)transformedValue:(id)value {
    return (value == nil) ? nil : NSStringFromClass([value class]);
  }
  @end
  </code></pre>
  
  <p>It seems it can store data into <code>NSString</code> (maybe I misunderstood..), so I tried like below:</p>
  
  <pre><code>@implementation ArrayToStringTransformer
  
  + (BOOL)allowsReverseTransformation {
    return YES;
  }
  
  + (Class)transformedValueClass {
    return [NSString class];
  }
  
  - (id)transformedValue:(id)value {
  //  return NSStringFromClass([value class]);
  //  return NSStringFromClass([@"11" class]);
  
    NSLog(@"!!!!!! %@, %@", NSStringFromClass([value class]), value);
    if ([value isKindOfClass:[NSString class]])
      return NSStringFromClass([value class]);
  
    NSMutableString * string = [NSMutableString string];
    for (NSNumber * number in value)
      [string appendString:[NSString stringWithFormat:@"%@,", number]];
    return string;
  }
  
  - (id)reverseTransformedValue:(id)value {
    NSArray * array = [value componentsSeparatedByString:@","];
    return array;
  }
  
  @end
  </code></pre>
  
  <p>However, it crashed with the error below (it includes the NSLog):</p>
  
  <pre><code>[26364:11903] !!!!!! __NSCFString, 0,0
  [26364:11903] -[__NSCFString bytes]: unrecognized selector sent to instance 0x905f620
  [26364:11903] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString bytes]: unrecognized selector sent to instance 0x905f620'
  </code></pre>
  
  <p>Any idea? Please!!</p>
  
  <hr>
  
  <h3>EDIT:</h3>
  
  <p>Well, I just transform <code>NSArray</code> to <code>NSData</code>, and if it is <code>NSString</code> type, transform it to <code>NSArray</code> first, then to <code>NSData</code>, and it works:</p>
  
  <pre><code>+ (Class)transformedValueClass {
    return [NSArray class];
  }
  
  - (id)transformedValue:(id)value {
    if (! [value isKindOfClass:[NSString class]])
      return [NSKeyedArchiver archivedDataWithRootObject:value];
  
    NSLog(@"!!! Convert NSString to NSArray");
    NSArray * array = [value componentsSeparatedByString:@","];
    NSData * data = [NSKeyedArchiver archivedDataWithRootObject:array];
    return data;
  }
  
  - (id)reverseTransformedValue:(id)value {
    return [NSKeyedUnarchiver unarchiveObjectWithData:value];
  }
  </code></pre>
  
  <h2>!!! Attention Here</h2>
  
  <p>But I wonder whether it is a right way? As you can see, I <code>return [NSArray class]</code> in <code>transformedValueClass</code> method, but actually return <code>NSData</code> type data value in <code>transformedValue:</code>.</p>
  
  <p>The apple DOC said:</p>
  
  <blockquote>
    <p>An NSData object containing the encoded form of the object graph whose root object is rootObject</p>
  </blockquote>
  
  <p>I'm totally confused...</p>