2010-05-13 49 views
0

所以我對這個iphone開發的東西很新....並且我被卡住了。iphone應用程序:代表沒有響應

我正在構建一個連接到twitter api的應用程序。但是,當調用connectionDidFinishLoading方法時,它似乎無法識別委託。

這裏的請求類的源代碼:

#import "OnePageRequest.h" 

@implementation OnePageRequest 

@synthesize username; 
@synthesize password; 
@synthesize receivedData; 
@synthesize delegate; 
@synthesize callback; 
@synthesize errorCallBack; 
@synthesize contactsArray; 

-(void)friends_timeline:(id)requestDelegate requestSelector:(SEL)requestSelector{ 
    //set the delegate and selector 
    self.delegate = requestDelegate; 
    self.callback = requestSelector; 

    //Set up the URL of the request to send to twitter!! 
    NSURL *url = [NSURL URLWithString:@"http://twitter.com/statuses/friends_timeline.xml"]; 

    [self request:url]; 
} 

-(void)request:(NSURL *) url{ 

    theRequest = [[NSMutableURLRequest alloc] initWithURL:url]; 
    theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

    if (theConnection){ 
     //Create the MSMutableData that will hold the received data. 
     //receivedData is declared as a method instance elsewhere 
     receivedData=[[NSMutableData data] retain]; 
    }else{ 
     //errorMessage.text = @"Error connecting to twitter!!"; 
    } 
} 

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{ 

    if ([challenge previousFailureCount] == 0){ 
     NSLog(@"username: %@ ",[self username]); 
     NSLog(@"password: %@ ",[self password]); 
     NSURLCredential *newCredential = [NSURLCredential credentialWithUser:[self username] 
                    password:[self password] 
                   persistence:NSURLCredentialPersistenceNone]; 
     [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge]; 
    } else { 
     [[challenge sender] cancelAuthenticationChallenge:challenge]; 
     NSLog(@"Invalid Username or password!"); 
    } 
} 

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

    [receivedData setLength:0]; 

} 

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ 

    [receivedData appendData:data]; 

} 

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ 

    [theConnection release]; 
    [receivedData release]; 
    [theRequest release]; 

    NSLog(@"Connection failed. Error: %@ %@", [error localizedDescription], [[error userInfo] objectForKey:NSErrorFailingURLStringKey]); 

    if(errorCallBack){ 
     [delegate performSelector:errorCallBack withObject:error]; 
    } 
} 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{ 

    if (delegate && callback){ 
     if([delegate respondsToSelector:[self callback]]){ 
      [delegate performSelector:[self callback] withObject:receivedData]; 
     }else{ 
      NSLog(@"No response from delegate!"); 
     } 
    } 
    [theConnection release]; 
    [theRequest release]; 
    [receivedData release]; 
} 

這裏的.H:

@interface OnePageRequest : NSObject { 

    NSString *username; 
    NSString *password; 
    NSMutableData *receivedData; 
    NSURLRequest *theRequest; 
    NSURLConnection *theConnection; 
    id delegate; 
    SEL callback; 
    SEL errorCallBack; 
    NSMutableArray *contactsArray; 
} 

@property (nonatomic,retain) NSString *username; 
@property (nonatomic,retain) NSString *password; 
@property (nonatomic,retain) NSMutableData *receivedData; 
@property (nonatomic,retain) id delegate; 
@property (nonatomic) SEL callback; 
@property (nonatomic) SEL errorCallBack; 
@property (nonatomic,retain) NSMutableArray *contactsArray; 

-(void)friends_timeline:(id)requestDelegate requestSelector:(SEL)requestSelector; 
-(void)request:(NSURL *) url; 

@end 

在該方法中:connectionDidFinishLoading,下面永遠不會被執行:

[delegate performSelector:[self callback] withObject:receivedData]; 

相反,我收到消息:「沒有代表的回覆」

有人看到我做錯了什麼?!或者可能導致問題的原因是什麼?

+0

您好Fiona,請編輯您的問題以正確格式化代碼。只需選擇部分代碼並使用「代碼」按鈕自動縮進。 – ohhorob 2010-05-13 15:15:15

回答

0
[delegate performSelector:[self callback] withObject:receivedData]; 

使用調試器connectionDidFinishLoading:您可以驗證(對不起,我對SEL參數評論,是由於難以閱讀粘貼到問題的代碼。)

delegatecallback變量不是零?


回調檢索它的值是:friends_timeline_callback,這正是我所期待

下一步是驗證選擇實際上是正確的。即如果回調方法有一個參數,它應該是@selector(friends_timeline_callback:)(注意尾隨:字符指定參數是方法簽名的一部分)

+0

嗨ohhorob ..在調試時,執行以下語句: if([delegate respondsToSelector:[self callback]]):回調超出了範圍......但是,當執行此語句並檢索到回調的值時它有價值:friends_timeline_callback,這是我所期望的。 – Fiona 2010-05-13 15:27:23