我正在使用AFNetworking
,並且我正在通過從google路線獲取路線,在iphone屏幕上繪製路線的教程。我正在使用JSON
和AFNetworking
。我在複製代碼教程,你可以在這裏找到:TutorialGDirections路線在地圖上查看崩潰
如果你也選擇複製和測試該代碼,只需注意:您需要的AFNetworking
從該github上頁: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
你們可以幫忙嗎?謝謝!
我想你沒有最新的AFNetworking代碼。你應該*不*修改AFNetworking的代碼! – Felix 2012-08-06 10:18:16
哦,對不起,讓我下載最新的代碼。 – MCKapur 2012-08-06 10:18:46
嘗試顯式創建一個'AFJSONRequestOperation'而不是使用'HTTPRequestOperationWithRequest' – Felix 2012-08-06 10:53:46