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 に答える