2

私は を使用してAFNetworkingおり、Google のルート案内からルートを取得して iPhone 画面にルートを描画する方法についてのチュートリアルに従っています。JSON、およびを使用してAFNetworkingいます。ここにあるチュートリアルからコードをコピーしました:チュートリアル

このコードもコピーしてテストすることを選択した場合は、注意してください:AFNetworkingこの github ページから必要です: AFNetworking ダウンロード

また、変数_pathNSMutableArray.h で自分で定義する必要があります。そうしないと、変数を定義していないが参照しているため、エラーが発生します。

コードは次のとおりです。

        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 に送信されました

手伝ってくれませんか?ありがとう!

4

2 に答える 2

2

AFHTTPClient には、以前はこの HTTPOperationWithRequest クラス メソッドがありませんでしたが、AFHTTPRequestOperation からコピー アンド ペーストする必要がありました。

AFNetworkingあなたのgithubリンクからクローンを作成し、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) {
        ;
    }];

ちなみに、リンクしたチュートリアルには正しくあります。

于 2012-08-06T10:23:37.227 に答える
0

少し遊んだら、わかりました。が含まれている場合routeのみになります。とこことの関係。だから、私はこれを知っていたので、以前からファイルを持っていました。しかし、それを受け入れなかったので、次の行を入れなければなりませんでした:NSDictionaryJSONKitAFNetworkingJSONKitJSONKitAFHTTPClient

        [_httpClient setDefaultHeader:@"Accept" value:@"application/json"];
于 2012-08-07T00:12:09.777 に答える