2012-07-24 67 views
2

我一直想這個Twitter的API的東西,這真是混亂...EXC_BAD_ACCESS -iPhone目標C

我不斷收到EXC_BAD_ACCESS壞訪問下面的代碼...這裏有什麼問題嗎?

NSURL *followingURL = [NSURL URLWithString:@"https://api.twitter.com/1/users/lookup.json"]; 
// Pass in the parameters (basically '.ids.json?screen_name=[screen_name]') 
id fromIntToNum = [NSNumber numberWithInteger: friID]; 
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:@"159462573", @"user_id", nil]; 
// Setup the request 
twitterRequest = [[TWRequest alloc] initWithURL:followingURL 
               parameters:parameters 
              requestMethod:TWRequestMethodGET]; 
// This is important! Set the account for the request so we can do an authenticated request. Without this you cannot get the followers for private accounts and Twitter may also return an error if you're doing too many requests 
[twitterRequest setAccount:theAccount]; 
// Perform the request for Twitter friends 
[twitterRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
    if (error) { 
     /* 
     // deal with any errors - keep in mind, though you may receive a valid response that contains an error, so you may want to look at the response and ensure no 'error:' key is present in the dictionary 
     NSLog(@"%@",error);*/ 
    } else { 
     /*NSError *jsonError = nil; 
     // Convert the response into a dictionary 
     NSDictionary *twitterGrabbedUserInfo = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONWritingPrettyPrinted error:&jsonError]; 
     // Grab the Ids that Twitter returned and add them to the dictionary we created earlier 
     NSLog(@"%@", [twitterGrabbedUserInfo objectForKey:@"screen_name"]);*/ 
    } 

}]; 

我分開我的代碼失敗就行了...

什麼可能會導致這Twitter的API問題?

下面一行導致崩潰:::

[twitterRequest performRequestWithHandler:^(NSData *responseData, 
       NSHTTPURLResponse *urlResponse, NSError *error) { 

我有時也會出現此錯誤:

[__NSCFNumber credentialForAccount:]: unrecognized 

更新:我註釋掉的處理程序和我做了TwitterRequest和伊娃,但它仍然崩潰...

+0

哪條線產生碰撞? – Vladimir 2012-07-24 14:30:38

+0

[twitterRequest performRequestWithHandler:^(NSData * responseData, NSHTTPURLResponse * urlResponse,NSError * error){ – 2012-07-24 14:31:14

回答

2

在您的區塊中查找錯誤,但是如果找到一個錯誤,則記錄它並繼續。你應該放入一個「else」語句,並且只在沒有錯誤的情況下繼續。

爲什麼不嘗試在處理程序中註釋掉所有的代碼 - 不要做任何事情 - 並查看是否會導致崩潰。然後嘗試使用錯誤代碼。然後嘗試使用JSON序列化,最後使用最後一行。如果您可以找到造成問題的塊的部分,將會有所幫助。

此外,我懷疑performRequestWithHandler:不會阻塞,但期望您通知您的類內的請求完成塊。如果是這樣,則意味着「TWRequest * twitterRequest」應該是ivar或property,並且您需要允許在處理程序完成時調用某個方法。您的崩潰可能是由於ARC在對象運行時重新分配對象。

編輯: 注意的是,TWRequest類描述說:「使用initWithURL:參數:requestMethod:方法來初始化傳遞所需的屬性值新創建TWRequest對象」它說複數形式的屬性,這意味着超過1。難道它也期望一個「credentialForAccount」屬性?您必須閱讀Twitter文檔才能找到所有必需的屬性。

編輯2: 那麼,我們甚至不知道你是否能夠像你的處理程序一樣。把NSLog放在那裏,但我懷疑它從來沒有那麼遠。如果真這樣離開三種可能性:

一)但這並沒有像URL(儘管這似乎不錯)

B)你缺少一些參數,它預計

C)id不喜歡「帳戶」對象 - 它是一個有效的ACAccount對象嗎?嘗試NSLogging它。

它必須是這三件事之一。

+0

我剛更新了問題,我仍然得到崩潰... – 2012-07-24 17:51:35

+0

它仍然無法工作... – 2012-07-24 20:28:51

+0

什麼是缺少的參數可以告訴我什麼參數是必需的? – 2012-07-25 00:58:52

1

我與iOS5和TWRequest有類似的問題,問題原來是在請求處理程序塊被調用之前被釋放的ACAccountStore。下面的代碼爲我解決了這個問題:

__weak ACAccountStore *wAccountStore = accountStore; 
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
    __strong ACAccountStore *sAccountStore = wAccountStore; 

    // handle the finished request here 

    // to silence Xcode warning 'unused variable' 
    // not necessary for releasing sAccountStore, 
    // it will go out of scope anyway when the block ends 
    sAccountStore = nil; 
}]; 

這樣,賬戶存儲被保留,直到塊完成。

+0

你是一個真正的男人!謝謝! – 2013-04-09 13:42:24