ios字典存bool_Objective-C的字典插入BOOL

OK, I'm a little confused.

It's probably just a triviality.

I've got a function which looks something like this:

- (void)getNumbersForNews:(BOOL)news andMails:(BOOL)mails {

NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];

[parameters setValue:news forKey:@"getNews"];

[parameters setValue:mails forKey:@"getMails"];...}

It doesn't matter whether I use setValue:forKey: or setObject:ForKey:, I'm always getting a warning:

"Passing argument 1 of set... makes pointer from integer without a cast"...

How on earth do I insert a bool into a dictionary?

解决方案

Values in an NSDictionary must be objects. To solve this problem, wrap the booleans in NSNumber objects:

[parameters setValue:[NSNumber numberWithBool:news] forKey:@"news"];

[parameters setValue:[NSNumber numberWithBool:mails] forKey:@"mails"];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以在 checkbox 的槽函数中根据选中状态来执行相应的查询。 例如,你可以在每个 checkbox 的槽函数中,判断该 checkbox 是否选中,如果选中则执行对应的数据库查询: ``` void MyWidget::on_checkBox_stu_num_toggled(bool checked) { if (checked) { QString str = ui->let_yyw_student_number->text(); // 执行学号查询 // ... } // 更新数据显示 updateTable(); } void MyWidget::on_checkBox_name_toggled(bool checked) { if (checked) { QString str = ui->let_yyw_name->text(); // 执行姓名查询 // ... } // 更新数据显示 updateTable(); } // 其他 checkbox 的槽函数类似 ``` 在每个槽函数中,你可以根据选中的 checkbox 执行对应的数据库查询,并使用 updateTable() 函数更新数据显示。在 updateTable() 函数中,你可以根据所有 checkbox 的选中状态,确定要显示的数据,然后更新数据显示。具体实现可以参考你提供的代码。 另外,你也可以使用 QButtonGroup 类来管理多个 checkbox,这样可以在一个槽函数中处理所有 checkbox 的状态变化。例如: ``` void MyWidget::onCheckBoxToggled(int id, bool checked) { switch (id) { case 1: if (checked) { QString str = ui->let_yyw_student_number->text(); // 执行学号查询 // ... } break; case 2: if (checked) { QString str = ui->let_yyw_name->text(); // 执行姓名查询 // ... } break; // 其他 checkbox 的查询处理 } // 更新数据显示 updateTable(); } // 在构造函数中创建 QButtonGroup 并连接槽函数 MyWidget::MyWidget(QWidget *parent) : QWidget(parent) { ui->setupUi(this); // 将多个 checkbox 添加到同一个 QButtonGroup QButtonGroup *group = new QButtonGroup(this); group->addButton(ui->checkBox_stu_num, 1); group->addButton(ui->checkBox_name, 2); // 添加其他 checkbox // 连接槽函数 connect(group, SIGNAL(buttonToggled(int,bool)), this, SLOT(onCheckBoxToggled(int,bool))); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值