2016-01-08 44 views
-2

我有以下代碼:錯誤的NSDictionary發生EXC_BAD_ACCESS

NSString * client_type = @"client_credentials"; 

@implementation OauthObject 

- (NSDictionary*) getParamsCredintion{ 

     return [[NSDictionary alloc] initWithObjectsAndKeys:client_id, @"client_id", client_secret, @"client_secret", client_type, "@client_credentials", nil]; 

} 

當我嘗試初始化的NSDictionary與client_type鍵,我得到錯誤:

NSDictionary EXC_BAD_ACCESS

+0

什麼是***確切***錯誤信息?無論如何,您應該嘗試使用Objective-C字典文字。 – luk2302

回答

1

你的錯誤是在(你報價後使用@)

client_type, "@client_credentials"

更改爲

client_type, @"client_credentials"

- (NSDictionary*) getParamsCredintion{ 

    return [[NSDictionary alloc] initWithObjectsAndKeys:client_id, @"client_id", client_secret, @"client_secret", client_type, @"client_credentials", nil]; 

} 
0

的NSDictionary不能存儲標量值(如BOOL, NSInteger等),它只能存儲對象。你可能擁有Numbers或Bools。

NSDictionary *qDictionary = [[NSDictionary alloc] 
    initWithObjectsAndKeys:question,@"questionText", 
          [NSNumber numberWithInteger:questionId],@"questionId", 
          [NSNumber numberWithBool:userAnswer],@"userAnswer", 
          [NSNumber numberWithBool:correctAnswer],@"correctAnswer", 
          nil]; 
0

什麼是getParam的意義sCredintion?如果編譯器有幽默感,那麼這就是你的代碼崩潰的原因......最有可能的client_id,client_secret,client_type之一是零或垃圾。顯然這不是你真正的代碼,對吧?

在創建字典的行上設置斷點並檢查值。建議將字典分配給一個變量並將其返回,以便您可以在那裏設置斷點,並檢查字典或記錄其值。讓你的代碼調試器友好。