我正在使用AFNetworking
,我正在通过从谷歌指示获取路线来关注在iphone屏幕上绘制路线的教程。我使用的是JSON
和AFNetworking
。我从您可以在此处找到的教程中复制了代码:Tutorial
如果您还选择复制并测试此代码,请注意:您需要此github页面中的AFNetworking
:AFNetworking Download
你还必须自己在.h中将变量_path
定义为NSMutableArray
,否则你会得到错误,因为它们没有定义但引用它。
以下是代码:
AFHTTPClient *_httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://maps.googleapis.com/"]];
[_httpClient registerHTTPOperationClass: [AFJSONRequestOperation class]];
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setObject:[NSString stringWithFormat:@"%f,%f", location.coordinate.latitude, location.coordinate.longitude] forKey:@"origin"];
[parameters setObject:[NSString stringWithFormat:@"%f,%f", location2.coordinate.latitude, location2.coordinate.longitude] forKey:@"destination"];
[parameters setObject:@"true" forKey:@"sensor"];
NSMutableURLRequest *request = [_httpClient requestWithMethod:@"GET" path: @"maps/api/directions/json" parameters:parameters];
request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
AFHTTPRequestOperation *operation = [AFHTTPClient HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id response) {
NSInteger statusCode = operation.response.statusCode;
if (statusCode == 200) {
[self parseResponse:response];
} else {
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { }];
[_httpClient enqueueHTTPRequestOperation:operation];
所以继承我的问题
感谢那些帮助过的人。我已经测试了没有错误的代码,但现在发现在尝试制作路线时。它在这里崩溃了:
- (void)parseResponse:(NSDictionary *)response {
NSArray *routes = [response objectForKey:@"routes"]; // CRASH HERE
NSDictionary *routePath = [routes lastObject];
if (routePath) {
NSString *overviewPolyline = [[routePath objectForKey: @"overview_polyline"] objectForKey:@"points"];
_path = [self decodePolyLine:overviewPolyline];
NSInteger numberOfSteps = _path.count;
CLLocationCoordinate2D coordinates[numberOfSteps];
for (NSInteger index = 0; index < numberOfSteps; index++) {
CLLocation *location = [_path objectAtIndex:index];
CLLocationCoordinate2D coordinate = location.coordinate;
coordinates[index] = coordinate;
}
MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
[self.mapView addOverlay:polyLine];
}
}
使用错误说明:
你能帮忙吗?谢谢!- [__ NSCFData objectForKey:]:无法识别的选择器发送到实例0x2004bb80
答案 0 :(得分:2)
我还要说AFHTTPClient之前没有这个 HTTPOperationWithRequest类方法如图所示,但我不得不复制和 从AFHTTPRequestOperation粘贴它。
我从您的github链接克隆了AFNetworking
并在AFHTTPClient.h
文件中找到了这个:
- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)request
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
我想知道你为什么会收到错误而不是“找不到方法”警告。 HTTPRequestOperationWithRequest
是实例方法,而不是类方法:
AFHTTPRequestOperation *operation = [_httpClient HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
// do something
;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
;
}];
顺便说一句,您链接的教程是正确的。
答案 1 :(得分:0)
好好玩了一下后我才明白了。如果包含route
,则NSDictionary
只会是JSONKit
。这里与AFNetworking
和JSONKit
有一些关系。所以,我之前已经有了JSONKit
个文件,因为我知道这个。但是AFHTTPClient
并没有接受它,所以我不得不说出这句话:
[_httpClient setDefaultHeader:@"Accept" value:@"application/json"];