【问题标题】:Appending and Updating in a dictionary having a Dictionary as` Value`在字典中追加和更新字典作为`值`
【发布时间】:2017-01-07 03:40:29
【问题描述】:

我想在字典的同一个键中添加多个值。 我有一个类型的 Dic

 var dic1 = [String:[String:AnyObject]](

 dic1.updateValue(["name": cellName,"phone": Phone], forKey: "XYZ")

通过它更新键“XYZ”的值,但我想在同一个键“XYZ”中添加更多值。 谁能帮我解决这个问题?

【问题讨论】:

  • 您需要添加该自定义运算符才能使用他的 += 建议 func +=<Key, Value> (inout lhs: [Key: Value], rhs: [Key: Value]) { rhs.forEach{ lhs[$0] = $1 } }
  • @LeoDabus 你能帮帮我吗?可以按步骤或任何适合您的方式进行
  • 这将解决错误 += cannot be used with two [String:AnyObject] 操作数
  • @LeoDabus 但我不知道你如何使用你的代码。请帮帮我。
  • @NavjotSingh 当答案满足您的需求时,考虑到您提供的信息量,您需要做的就是接受并投票。您还需要了解 SO 不是个人帮助中心,为什么人们在这里回答主要是为了帮助可能最终解决您的问题的其他人,这就是为什么我们尝试尽可能概括我们的答案,或者如果您想具体回答然后提供您的具体细节。请查收:-stackoverflow.com/help/someone-answers.

标签: ios swift dictionary


【解决方案1】:

你的意思是这样吗?

var value = Dict1["XYZ"]
value["address"] = "AAA BBB CCC 1234"
Dict1["XYZ"] = value

【讨论】:

    【解决方案2】:

    如果我理解了这个问题:

    1) 你必须定义一个类,你可以在里面放很多值。示例:

     var user_location, user_mail, user_name, user_pass, user_urlPicture: String!
    
    init(user_location: String!, user_mail: String!, user_name: String!, user_pass: String!, user_urlPicture: String!){
    
        self.user_location = user_location
        self.user_mail = user_mail
        self.user_name = user_name
        self.user_pass = user_pass
        self.user_urlPicture = user_urlPicture
    
    }
    // DictionaryConvertible protocol methods
    required convenience init?(dict: [String:AnyObject]) {
        guard let user_location = dict["user_location"] as? String, user_mail = dict["user_mail"] as? String, user_name = dict["user_name"] as? String, user_pass = dict["user_pass"] as? String, user_urlPicture = dict["user_urlPicture"] as? String else {
            return nil
        }
        self.init(user_location: user_location, user_mail: user_mail, user_name: user_name, user_pass: user_pass, user_urlPicture: user_urlPicture)
    }
    var dict:[String:AnyObject] {
        return [
            "user_location": user_location,
            "user_mail": user_mail,
            "user_name": user_name,
            "user_pass": user_pass,
            "user_urlPicture": user_urlPicture
        ]
    }
    

    2) 在您的 ("User") 类中,您可以添加您的值:

    let user = User(yourLocation, user_mail: yourMail, user_name: yourName, user_pass: yourPass, user_urlPicture: yourUrlPicture)
    

    【讨论】:

      【解决方案3】:

      一个简单而优雅的解决方案:-

      var mutDictionary = [String: NSMutableDictionary]()
      
      //Initialising your mutDictionary:-
      mutDictionary = ["Fighters" : ["First" : ["NAME" : "The Tigress", "Phone" : "2131231231"]]]
      
      //Adding a key-hashabale(or in this case String-NSMutableDictionary) pair.  
      //`.updateValue` adds a new parent pair to your mutDictionary if your key value is not present if the key is present then it will update that key's value.
      mutDictionary.updateValue(["First" : ["NAME" : "Uruguay", "Phone" : "903192301293"]], forKey: "The Bosses")
      mutDictionary.updateValue(["FirstOfHisKind" : ["NAME" : "Panda", "Phone" : "123454362"]], forKey: "The Dragon Warrior")    
      
      //Appending data to your mutDictionary:-
      mutDictionary["Fighters"]?.setObject(["NAME" : "Guru Shifu", "Phone" : "121212121212"], forKey: "Second")
      
       print(mutDictionary)
      

      你从中得到的输出是这样的:-

         ["Fighters"          : { Second = {  NAME = Guru Shifu; 
                                              Phone = 121212121212;};
                                  First = {  NAME = The Tigress;  
                                             Phone = 2131231231;};
                                    },
          "The Bosses"        : {First = { NAME = Uruguay;
                                           Phone = 903192301293;};
                                    },
          "The Dragon Warrior": {First = { NAME = Panda;
                                           Phone = 123454362;};
                                  }]
      

      【讨论】:

      • 非常感谢朋友。它不完全是我想要的,但给了我一个想法,我得到了理想的输出。我正在上传它作为答案
      • @NavjotSingh 当答案满足您的需求时,考虑到您提供的信息量,您需要做的就是接受并投票。您还需要了解 SO 不是个人帮助中心,为什么人们在这里回答主要是为了帮助可能最终解决您的问题的其他人,这就是为什么我们尝试尽可能概括我们的答案,或者如果您想具体回答然后提供您的具体细节,您没有不。请查收:-stackoverflow.com/help/someone-answers.
      • 你能帮我按“name”键对这个数组“mutDictionary”进行排序
      • 这完全是一个不同的问题。避免在 cmets 中建立问题,在网上搜索它们,如果您没有找到任何解决方案,请在 SO 上提出一个问得很好的问题。
      【解决方案4】:
              var mutDictionary = [String: NSMutableDictionary]()
              let keyVar = String(cellName[cellName.startIndex])
              if mutDictionary[keyVar] == nil {
                mutDictionary.updateValue([:], forKey: keyVar)
                mutDictionary[keyVar]?.setObject(["Name" : cellName, "Phone" : Phone], forKey: cellName)
              }
              else{
                mutDictionary[keyVar]?.setObject(["Name" : cellName, "Phone" : Phone], forKey: cellName)
              }
          print(mutDictionary)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-15
        • 1970-01-01
        • 2015-02-04
        • 1970-01-01
        • 2012-03-10
        • 2018-06-22
        • 1970-01-01
        相关资源
        最近更新 更多