JSON多个键指向一个值

时间:2019-06-07 14:45:46

标签: json dictionary

我希望能够从JSON文件中的多个键访问相同的数据。

class MyTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.addButton()
    }

    func addButton() {
        let button = UIButton(type: UIButton.ButtonType.custom)
        button.bounds = CGRect(x:0,y:0,width:50,height:50);  //1
        button.backgroundColor = UIColor.purple
         button.center = CGPoint(x:self.tabBar.frame.size.width/2, y:self.tabBar.frame.size.height/2 - 50 + self.tabBar.frame.origin.y);  //2
         button.addTarget(self, action: #selector(click(button:)), for: UIControl.Event.touchUpInside)

         button.layer.cornerRadius = button.frame.size.height/2
         button.layer.masksToBounds = false
         button.layer.shadowColor = UIColor.black.withAlphaComponent(0.5).cgColor
         button.layer.shadowRadius = 5.0
         button.layer.shadowOffset = CGSize(width: 0.0, height: 5.0)
         button.layer.shadowOpacity = 0.5

        //tabBar.addSubview(button)  //3
         self.view.addSubview(button). //4
    }

    @objc func click(button: UIButton) {
         print("Button get clicked")
    }
}

实际问题更加复杂,数据是序列化对象。但这是当前问题的症结所在。

2 个答案:

答案 0 :(得分:1)

您可能会遇到类似的事情:

{
   "TypeA" : "COMMON_TYPE_VALUE",
   "TypeB" : "COMMON_TYPE_VALUE",
   "TypeC" : "COMMON_TYPE_VALUE",
   "COMMON_TYPE_VALUE" : "here is the common value"
}

您当然必须先了解所有常见键...

答案 1 :(得分:1)

听起来像您可能需要使其具有关联性。

"Types": [
  "These types are not supported",
  "Vroom vroom!"
],
...
"TypeA": 0,
"TypeB": 0,
"TypeC": 0,
"TypeR": 1

也许不是最好的结构,但突出了这个想法。