概要。いくつかのベンチマークに基づいて、iPhoneJSONパーサーにyajl-objcを選択しました。任意のカスタムクラス(1つのNSNumberプロパティと2つのNSStringプロパティ)を使用してテストしていました。クラスのプロパティに一致するキーと値のペアを使用してNSDictionaryを作成した場合、辞書を。でエンコードでき[dictionary yajl_JSON]
ます。カスタムクラスのインスタンスをで直接エンコードしようとすると[custom yajl_JSON]
、次のコンパイラエラーが発生しました。
Terminating app due to uncaught exception 'YAJLParsingUnsupportedException', reason: 'Object of type (Custom) must implement dataUsingEncoding: to be parsed'
。
- (id)JSON
(yajl-objc readmeで提案されているように)実装してもエラーが発生しました。ライブラリではなく、自分のコードが問題であることを私は知っています。何が間違っているのか理解できません。
詳細。私のCustom.h:
#import
@interface Custom : NSObject {
NSString *name;
NSNumber *number;
NSString *text;
}
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSNumber *number;
@property (nonatomic, copy) NSString *text;
@end
- (id)JSON
Custom.mで、yajl-objドキュメントに従ってメソッドを定義しました。
- (id)JSON
{
NSDictionary *jsonDictionary =
[[[NSMutableDictionary alloc] init] autorelease];
[jsonDictionary setValue:name forKey:@"name"];
[jsonDictionary setValue:number forKey:@"number"];
[jsonDictionary setValue:text forKey:@"text"];
return jsonDictionary;
}
Key-Value一致辞書を作成し、それをエンコードすると、正常に機能します。
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setValue:self.custom.name forKey:@"name"];
[dict setValue:self.custom.number forKey:@"number"];
[dict setValue:self.custom.text forKey:@"text"];
NSString *JSONString = [dict yajl_JSON];
しかし、私が電話するときNSString* JSONString = [custom yajl_JSON]
; これが私が得るコンパイラエラーとスタックトレースです:
*** Terminating app due to uncaught exception 'YAJLParsingUnsupportedException', reason: 'Object of type (Custom) must implement dataUsingEncoding: to be parsed'
*** Call stack at first throw:
(
0 CoreFoundation 0x0266bb99 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x027bb40e objc_exception_throw + 47
2 CoreFoundation 0x02624238 +[NSException raise:format:arguments:] + 136
3 CoreFoundation 0x026241aa +[NSException raise:format:] + 58
4 TestCustomFileFormat 0x00005151 -[NSObject(YAJL) yajl_JSONWithOptions:error:] + 206
5 TestCustomFileFormat 0x00005212 -[NSObject(YAJL) yajl_JSON:] + 49
6 TestCustomFileFormat 0x00005249 -[NSObject(YAJL) yajl_JSON] + 49
7 TestCustomFileFormat 0x00002eba -[TestViewController loadInstruction] + 758
8 TestCustomFileFormat 0x00002aa5 -[TestViewController viewDidLoad] + 77
9 UIKit 0x0037585a -[UIViewController view] + 179
10 TestCustomFileFormat 0x00002377 -[TestCustomFileFormatAppDelegate application:didFinishLaunchingWithOptions:] + 112
11 UIKit 0x002cc6a7 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1163
12 UIKit 0x002ceb30 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 346
13 UIKit 0x002d8b6c -[UIApplication handleEvent:withNewEvent:] + 1958
14 UIKit 0x002d12bc -[UIApplication sendEvent:] + 71
15 UIKit 0x002d613f _UIApplicationHandleEvent + 7672
16 GraphicsServices 0x02f4b81e PurpleEventCallback + 1578
17 CoreFoundation 0x0264cff4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
18 CoreFoundation 0x025ad807 __CFRunLoopDoSource1 + 215
19 CoreFoundation 0x025aaa93 __CFRunLoopRun + 979
20 CoreFoundation 0x025aa350 CFRunLoopRunSpecific + 208
21 CoreFoundation 0x025aa271 CFRunLoopRunInMode + 97
22 UIKit 0x002ce3ed -[UIApplication _run] + 625
23 UIKit 0x002da272 UIApplicationMain + 1160
24 TestCustomFileFormat 0x000022e4 main + 102
25 TestCustomFileFormat 0x00002275 start + 53
)
terminate called after throwing an instance of 'NSException'
それに応じて、NSCodingに準拠しようとし、NSKeyedArchiverを使用してクラスインスタンスのNSMutableData表現を作成し、を呼び出しました[data yajl_JSON]
が、それも機能しませんでした。
単純なものが欠けていることは知っていますが、それを理解するには愚かすぎます。