3

次の2つの方法があります。

-(void)authenticateUserToGoogle:(NSString *)userName withPassword:(NSString *)password {


    NSString *URLstr = GOOGLE_CLIENT_LOGIN;
    URLstr = @"http://www.google.com/ig/api?stock=AAPL";
    NSURL *theURL = [NSURL URLWithString:URLstr];
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:theURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:100.0];

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if (!theConnection) {
        NSLog(@"COuldn't register device information with Parking Server");
    } else {
        NSLog(@"Got a connection!!");
        NSMutableData       *_responseData = [NSMutableData data];
        NSLog(@"respone_data = %@",_responseData);

    }
    }

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
    NSInteger statusCode = [HTTPResponse statusCode];

    if (404 == statusCode || 500 == statusCode) {
        //[self.controller setTitle:@"Error Getting Parking Spot ....."];
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE];
        NSLog(@"GOT A 'FUCKED' STATUS CODE");

        [connection cancel];
        NSLog(@"Server Error - %@", [NSHTTPURLResponse localizedStringForStatusCode:statusCode]);
    } else if (200 == statusCode) {
        NSLog(@"GOT A 'OK' RESPONSE CODE");

    }

}

次のように、authenticateUserToGoogle メソッドをインスタンス メソッドとして呼び出す場合:

[self authenticateUserToGoogle:user withPassword:password]

次の出力が得られます。

2011-08-12 00:14:08.490 stcoks[81272:f203] Got a connection!!
2011-08-12 00:14:08.492 stcoks[81272:f203] respone_data = <>
2011-08-12 00:14:08.726 stcoks[81272:f203] GOT A 'OK' RESPONSE CODE

ただし、メソッド シグネチャで「-」を「+」に変更するだけで、authenticateUserToGoogle メソッドをクラス メソッドに変更し、次のように呼び出すとします。

[MasterViewController authenticateUserToGoogle:user withPassword:password]

次の出力が得られます。

2011-08-12 00:14:08.490 stcoks[81272:f203] Got a connection!!
2011-08-12 00:14:08.492 stcoks[81272:f203] respone_data = <>

つまり、クラス メソッドでは、デリゲート メソッド接続 didReceiveResponse が呼び出されることはありません!!

誰かが私にこの動作を説明できますか? ありがとう!

4

4 に答える 4

5

で NSURLConnection を開始すると、メッセージdelegate:selfを受信するデリゲート オブジェクトが設定されconnection:didReceiveResponse:ます。クラスメソッド内で使用するselfと、このメソッドもクラスメソッドとして呼び出されます。

于 2011-08-12T04:26:57.293 に答える
2

メソッドシグネチャのtoをauthenticateUserToGoogle変更するだけでメソッドをクラスメソッドに変更する場合は、デリゲートメソッドのもに変更します。つまり、コードは次のようになります。"-""+"connection:didReceiveResponse:"-""+"

+ (void)authenticateUserToGoogle:(NSString *)userName withPassword:(NSString *)password {

    // Your code here
}

+ (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    // Your code here
}
于 2011-08-12T04:48:55.387 に答える
2

メソッドでデリゲートを self に設定しています。クラス メソッドの場合、self はクラスのインスタンスを保持しません。クラス自体か、nilのどちらかだと思います。

両方をクラス メソッドに変更してみてください。

于 2011-08-12T04:26:20.877 に答える
1

デリゲート メソッドもクラス メソッドに変更しない限り、デリゲート (クラス) はメッセージに応答せず、そのインスタンスのみが応答するため、呼び出されません。

于 2011-08-12T04:26:58.977 に答える