내 API 호출 중 하나에서 응답 객체로 고생하고 있습니다. 괜찮아요,하지만 개체 또는 countForKey 작업을 수행하려고하면 "인식 할 수없는 선택기 인스턴스로 보낸"오류가 발생합니다. 나는 응답 객체를 정확하게 디코딩하지 못한다고 느낀다. 어떤 입력이라도 크게 감사 할 것이다! API 호출에 대한응답 개체에서 작업을 수행 할 수 없습니까?
방법 :
- (void)callRegisterAccount:(NSString *)email
password:(NSString *)password
confirmPassword:(NSString *)confirmPassword
completionBlock:(void (^)(NSMutableArray *resultsArray))completion{
NSLog(@"REGISTER ACCOUNT CALLED!");
NSString *appendUrl = [NSString stringWithFormat:@"Account/Register"];
NSURL *aUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@""%@", @"xxx", appendUrl]];
NSLog(@"URL: %@",aUrl);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:30.0];
[request setHTTPMethod:@"POST"];
[request addValue:@"application/json, text/plain, */*" forHTTPHeaderField:@"Accept"];
[request addValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
//build an info object and convert to json
NSDictionary* info = [NSDictionary dictionaryWithObjectsAndKeys:
email,
@"Email",
password,
@"Password",
confirmPassword,
@"ConfirmPassword",
nil];
//convert object to data
NSError *error;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:info
options:NSJSONWritingPrettyPrinted error:&error];
NSString *strData = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"%@",strData);
[request setHTTPBody:[strData dataUsingEncoding:NSUTF8StringEncoding]];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
// NSLog(@"REGISTER PUSH NOTIFICATIONS RESPONSE: %@", response);
// NSLog(@"REGISTER PUSH NOTIFICATIONS ERROR: %@", error);
// NSLog(@"REGISTER PUSH NOTIFICATIONS DATA: %@", data);
NSData *_data = data;
NSMutableString *_string = [NSMutableString stringWithString:@""];
for (int i = 0; i < _data.length; i++) {
unsigned char _byte;
[_data getBytes:&_byte range:NSMakeRange(i, 1)];
if (_byte >= 32 && _byte < 127) {
[_string appendFormat:@"%c", _byte];
} else {
[_string appendFormat:@"[%d]", _byte];
}
}
NSLog(@"REGISTER ACCOUNT RESPONSE: %@", _string);
if(_string) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
completion((NSMutableArray*)_string);
}
}];
}