0

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

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

1 回答 1

2

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

使用字典接口:导入 JSON.h 然后:

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

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

ps NSLog 接受一种格式,因此您不需要从格式中创建一个字符串来传递给它。

于 2011-06-21T20:32:28.213 回答