尝试使用SBJsonWriter设置值/密钥对时出错

问题描述

|| 尝试设置值/键对时出现以下错误
2011-06-21 16:21:16.727 agent[94408:207] *** Terminating app due to uncaught exception \'NSUnkNownKeyException\',reason: \'[<SBJsonWriter 0xab31230> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key key.\'
这是代码
SBJsonWriter *writer = [[SBJsonWriter alloc] init];

[writer setValue:@\"val\" forKey:@\"key\"];

Nsstring *json = [writer JSONRepresentation];

NSLog([Nsstring stringWithFormat:@\"json: @%\",json]); 
    

解决方法

        使用using2ѭ类别的键值编码, 使用字典界面: 导入JSON.h然后:
    NSMutableDictionary * dict = [NSMutableDictionary new];
    [dict setValue:@\"val\" forKey:@\"key\"];

    NSLog(@\"json: %@\",[dict JSONRepresentation]);
ps。 NSLog接受格式,因此您无需从格式中生成字符串即可传递给它。