2011-03-23 64 views
2

我正在开发一个小应用程序的iPad,我试图序列化字典到NSData保存在磁盘。我正在使用框架TouchJson。我的示例结构的 而且例如:序列化与TouchJson字典的问题

{ 
line =  { 
    78986928 =   (
     "NSPoint: {442, 266}", 
     (...) 
     "NSPoint: {370, 634}" 
    ); 
}; 

}

我的字典的结构是:用内部词典的词典。这个字典有一个字符串(ID)和一个带NSValue的NSMutableArray。

的代码行的是,我使用的是:

NSData *jsonData = [[CJSONSerializer serializer] serializeObject:templates error:&error];

,变量错误给我的错误是:

2011-03-23 10:12:12.957 GestureFramework[286:207] Error Domain=TODO_DOMAIN Code=-1 "Could not serialize object '{ 
line =  { 
    78986928 =   (
     "NSPoint: {442, 266}", 
     (...) 
     "NSPoint: {370, 634}" 
    ); 
}; 
}'" UserInfo=0x4e27aa0 {NSLocalizedDescription=Could not serialize object '{ 
    line =  { 
     78986928 =   (
      "NSPoint: {442, 266}", 
      (...) 
      "NSPoint: {370, 634}" 
     ); 
    }; 
}'} 

日Thnx提前

回答

4

TouchJSON支持序列化以下类型:

  • NSNull
  • 的NSNumber
  • 的NSString
  • 的NSArray
  • 的NSDictionary
  • NSData的

如果您想序列化另一种类型,你需要实现-(NSData*)JSONDataRepresentation(无论是在子类或类别)。

下面是我用NSDate一个例子:

@interface NSDate (JSONDataRepresentation) 
- (NSData*)JSONDataRepresentation; 
@end 

@implementation NSDate (JSONDataRepresentation) 
- (NSData*)JSONDataRepresentation 
{ 
    return [@"\"didn't want to waste the space to do the real conversion\"" dataUsingEncoding: NSUTF8StringEncoding]; 
} 
@end