为什么我不能转换混合类型字典的值并更新该值? [英] Why can't I cast a value of a mixed type dictionary and update the value?

查看:57
本文介绍了为什么我不能转换混合类型字典的值并更新该值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图回答另一个StackOverflow问题,但发现我对Swift缺乏了解.

下面是演示我遇到的问题的操场代码.

  import Foundation//如何在不替换字典的情况下访问字典的值并更新该值?//如何使内部字典可变?var sampleDict = ["dictionary":[String:String](),"array":[String]()](sampleDict ["dictionary"] as![String:String]).updateValue("avalue",forKey:"akey")//错误:Swift.playground:12:27:错误:类型[[String:String]]的不可变值仅具有名为'updateValue'的变异成员//(sampleDict ["dictionary"] as![String:String]).updateValue("avalue",forKey:"akey")(sampleDict ["dictionary"] as![String:String])["akey"] ="avalue"//错误:Swift.playground:14:56:错误:无法分配给此表达式的结果//(sampleDict ["dictionary"] as![String:String])["akey"] ="avalue" 

我的问题是

  • 我如何访问mixedType词典的值并更新该值,而不替换它?
  • 如何使内部字典可变?
  • 这根本不可能吗?

解决方案

我认为这是不可能的.当查看 Dictionary 声明时,似乎没有方法可以更改值,只能替换它.之所以有意义,是因为当您获得一个值时,它会在字典中返回真实结构的副本(结构总是被复制).当然,使用类时它会起作用.那是我的理论.

我刚刚发现它确实可以正常工作,而无需强制转换为[String:String]和仅具有正确类型的字典:

  var sampleDict = ["dictionary":[String:String]()]sampleDict ["dictionary"] ?. updateValue("avalue",forKey:"akey")sampleDict ["dictionary"]?["akey"] ="avalue" 

I was attempting to answer another StackOverflow question and found my knowledge of Swift lacking.

Below is playground code that demonstrates the issue I'm having.

import Foundation

// How do I access the value of dictionary and update the value, without replacing it?
// How do I make the inner dictionary mutable?

var sampleDict = [
    "dictionary" : [String: String](),
    "array" : [String]()
]


(sampleDict["dictionary"] as! [String:String]).updateValue("avalue", forKey: "akey")
// ERROR: Swift.playground:12:27: error: immutable value of type '[String : String]' only has mutating members named 'updateValue'
//(sampleDict["dictionary"] as! [String:String]).updateValue("avalue", forKey: "akey")

(sampleDict["dictionary"] as! [String:String])["akey"] = "avalue"
// ERROR: Swift.playground:14:56: error: cannot assign to the result of this expression
// (sampleDict["dictionary"] as! [String:String])["akey"] = "avalue"

My questions are

  • How do I access the value of a mixedType dictionary and update the value, without replacing it?
  • How do I make the inner dictionary mutable?
  • Is this simply impossible?

解决方案

I think it's impossible. When looking at the Dictionary declaration, there seems to be no method to changing a value, only replacing it. It kind of makes sense because when you get a value, it returns a copy of the real struct in the dictionary (structs are always copied). Of course it would work when using a class. That's my theory..

EDIT: I just found out that it does work without the cast to [String: String] and a dictionary with just the right type:

var sampleDict = ["dictionary" : [String: String]()]
sampleDict["dictionary"]?.updateValue("avalue", forKey: "akey")
sampleDict["dictionary"]?["akey"] = "avalue"

这篇关于为什么我不能转换混合类型字典的值并更新该值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆