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

时间:2011-06-21 20:26:48

标签: iphone objective-c json sbjson

尝试设置值/密钥对时出现以下错误:

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]); 

1 个答案:

答案 0 :(得分:2)

正在使用(NSObject)类别中的键值编码,

使用字典界面: 然后导入JSON.h:

    NSMutableDictionary * dict = [NSMutableDictionary new];
    [dict setValue:@"val" forKey:@"key"];

    NSLog(@"json: %@", [dict JSONRepresentation]);

P.S。 NSLog接受一种格式,因此您无需从格式中生成字符串即可传递给它。