NSDictionary 中发生错误 EXC_BAD_ACCESS

Error occured in 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];

}

当我尝试使用 client_type 键初始化 NSDictionary 时出现错误:

NSDictionary EXC_BAD_ACCESS

NSDictionary 不能存储标量值(如 BOOL、NSInteger 等),它只能存储对象。您可能有数字或布尔值。

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

你的错误在(你在引号后面用了@)

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]; 

}

getParamsCredintion是什么意思?如果编译器有幽默感,那么这就是让你的代码崩溃的原因...... client_id、client_secret、client_type 中的一个很可能是 nil 或垃圾。显然这不是您的真实代码,对吧?

在创建字典的行上设置断点,并检查值。建议将字典分配给一个变量 return ,这样您也可以在那里设置断点并检查字典或记录其值。使您的代码调试器友好。