2012-08-06 16 views
2

我正在使用AFNetworking,並且我正在通過從google路線獲取路線,在iphone屏幕上繪製路線的教程。我正在使用JSONAFNetworking。我在複製代碼教程,你可以在這裏找到: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

你們可以幫忙嗎?謝謝!

+0

我想你沒有最新的AFNetworking代碼。你應該*不*修改AFNetworking的代碼! – Felix 2012-08-06 10:18:16

+0

哦,對不起,讓我下載最新的代碼。 – MCKapur 2012-08-06 10:18:46

+0

嘗試顯式創建一個'AFJSONRequestOperation'而不是使用'HTTPRequestOperationWithRequest' – Felix 2012-08-06 10:53:46

回答

0

玩了一下後好了我明白了。如果包含JSONKitroute只會是NSDictionary。這裏有一些與AFNetworkingJSONKit的關係。所以,我之前已經有了JSONKit的文件,因爲我知道這一點。但AFHTTPClient是不接受它,所以我不得不把它放在線:

 [_httpClient setDefaultHeader:@"Accept" value:@"application/json"]; 
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) { 
     ; 
    }]; 

順便說一句,你掛教程正確的有它。

+0

是的,我的AFNetworking代碼是從我的舊項目中拖入的,這意味着它是舊代碼。我將重新下載AFNetworking項目。我使它成爲類方法,因爲當我試圖使它成爲一個實例方法時,我得到了錯誤 – MCKapur 2012-08-06 10:28:21

+0

你可以爲我測試這個代碼嗎?我更新了我的問題關於路線崩潰 – MCKapur 2012-08-06 10:42:59

+0

我怎樣才能創建一個AFJSONRequestOperation而不是使用HTTPRequestOperationWithRequest – MCKapur 2012-08-06 12:11:18