型号:
Id
当车辆myvehicle
被推入func showAlertController()
{
//simple alert dialog
let alertController = UIAlertController(title: "", message: "", preferredStyle: UIAlertControllerStyle.alert);
// Add Action
alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: nil));
//show it
let btnImage = UIImage(named: "checkBoxImage")!
let imageButton : UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
imageButton.setBackgroundImage(btnImage, for: UIControlState())
imageButton.addTarget(self, action: #selector(ViewController.checkBoxAction(_:)), for: .touchUpInside)
alertController.view.addSubview(imageButton)
self.present(alertController, animated: false, completion: { () -> Void in
})
}
func checkBoxAction(_ sender: UIButton)
{
if sender.isSelected
{
sender.isSelected = false
let btnImage = UIImage(named: "checkBoxImage")!
sender.setBackgroundImage(btnImage, for: UIControlState())
}else {
sender.isSelected = true
let btnImage = UIImage(named: "unCheckBoxImage")!
sender.setBackgroundImage(btnImage, for: UIControlState())
}
}
字段时,它的值为空。
如何为嵌套的Vehicle的id设置一个值?
答案 0 :(得分:2)
对于未指定_id
字段的文档(非子文档),会自动插入MongoDB ObjectId。这是为了唯一地标识文档。
在上面的例子中,如果要插入嵌套对象(子文档),MongoDB将不会自动插入Id
字段。
但是,您可以为新推送的Vehicle文档创建ObjectId(唯一标识符)。例如:
new_object_id := bson.NewObjectId()