我有以下代码,但有以下错误,我不确定我做错了什么:
NSDictionary *jsonDict = [NSDictionary dictionaryWithObjectsAndKeys: @"init", @"method", nil];
NSString *requestString = [NSString stringWithFormat:@"json=%@", [jsonDict JSONFragment], nil];
NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://exampleurl.com/json.php"]];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: requestData];
NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
NSLog(@"Returned Json: %@", returnString);
我在运行Instance Method '-JSONFragment' not found(return type defaults to 'id')
之前收到以下错误,这表示我没有从SBJSON添加所有正确的文件,但所有文件都在那里。
当我运行应用程序时,我收到以下错误消息:
-[__NSCFDictionary JSONFragment]: unrecognized selector sent to instance 0x4e99010
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary JSONFragment]: unrecognized selector sent to instance 0x4e99010'
我不知道我做错了什么,有什么建议吗?
答案 0 :(得分:0)
我的SBJson版本没有名为JSONFragment的方法。这就是我将字典转换为Json格式的请求字符串的方法。
// assume dictionary has the values you want to post
SBJsonWriter *writer = [SBJsonWriter new];
NSString * jsonData = [writer stringWithObject:dictionary];
NSString * getString = [NSString stringWithFormat:@"json=%@", jsonData];
[writer release];