![](/img/trans.png)
[英](key: AnyObject, value: AnyObject) is not convertible to 'Dictionary<String, AnyObject>
[英]How to add a key value pair to a dictionary of type [String:AnyObject] in Swift?
我已将 JSON 文件的内容存储在 [[String:AnyObject]] 类型的字典数组中。 我想为 [String:AnyObject] 字典中的每个项目添加一个新的键值对。 我不断收到错误消息:无法分配给类型为“AnyObject?!”的不可变表达式有没有办法解决?
已声明主题如下:
var subjects = [[String:AnyObject]]()
编辑:抱歉没有发布代码。所以,这是 [[String:AnyObject]] 数组的元素之一
{ "subjects" : {
"humanities" : [
{"sno" : "TH5", "code" : 205, "name" : "Mathematics III", "credits" : 4}
],
"applied" : [
{"sno" : "TH3", "code" : 203, "name" : "Power Apparatus", "credits" : 4},
{"sno" : "TH4", "code" : 204, "name" : "Electrical Measurements", "credits" : 4}
],
"core" : [
{"sno" : "TH1", "code" : 201, "name" : "Electronics I", "credits" : 4},
{"sno" : "TH2", "code" : 202, "name" : "Circuits and Systems", "credits" : 4}
],
"theory" : [
{"sno" : "TH1", "code" : 201, "name" : "Electronics I", "credits" : 4, "category" : "C"},
{"sno" : "TH2", "code" : 202, "name" : "Circuits and Systems", "credits" : 4, "category" : "C"},
{"sno" : "TH3", "code" : 203, "name" : "Power Apparatus", "credits" : 4, "category" : "A"},
{"sno" : "TH4", "code" : 204, "name" : "Electrical Measurements", "credits" : 4, "category" : "A"},
{"sno" : "TH5", "code" : 205, "name" : "Mathematics III", "credits" : 4, "category" : "H"}
],
"practical" : [
{"sno" : "PR1", "code" : 206, "name" : "Electronics I", "credits" : 2},
{"sno" : "PR2", "code" : 207, "name" : "Power Apparatus", "credits" : 2},
{"sno" : "PR3", "code" : 208, "name" : "Electrical Measurements", "credits" : 2},
{"sno" : "PR4", "code" : 209, "name" : "Machine Drawing", "credits" : 3},
{"sno" : "VS1", "code" : 210, "name" : "Programming I", "credits" : 1}
]
},
"totalCredits" : 30,
"semester" : 3
}
八本这样的词典存储在“主题”中。 我想向“人文”、“应用”等中的每个元素添加另一个键值对“标记”:0。 这是我使用的代码,例如,“人文”
for i in 0..<self.subjects.count
{
for j in 0..<self.subjects[i]["humanities"]!.count
{
self.subjects[i]["humanities"]![j]["marks"] = 0
}
}
这就是我得到错误的地方。
编辑 2:添加了附加声明
主要问题是您没有像在数组中那样为字典赋值。
如果您像这样访问字典中的值:
var dictionaryValues = dictionary.values
然后尝试更改该数组 'dictionaryValues' 中的值,您试图错误地更新字典。
更改字典中条目的方法是根据其关联的键分配值。
所以我知道你想要更改的值的键,你可以像这样分配新值:
let key = "keyForValueYouWishToChange"
let newValue: AnyObject = 1
for (index, dictionary) in arrayOfDictionaries.enumerate() {
var mutableDictionary = dictionary
mutableDictionary[key] = newValue //this is how you assign to dictionaries
arrayOfDictionaries[index] = mutableDictionary
}
此外,将您的字典更改为 var 而不是 let,因此它是可变的
很难理解你真正想要做什么。 您不会在内存中保留像[String : [String : [[String : Any]]]]
的结构并将内容修改为该结构,这很可能是您收到此错误的原因。 你必须从这个结构中“提取”出来并使用它。 如果您想将其恢复为 JSON,则必须再次将其序列化,反之亦然。 首先,我将向您展示如何从 JSON 中获取单个主题:
// Mockup String
let jsonString = "{ \"subjects\" : { \"humanities\" : [ {\"sno\" : \"TH5\", \"code\" : 205, \"name\" : \"Mathematics III\", \"credits\" : 4} ], \"applied\" : [ {\"sno\" : \"TH3\", \"code\" : 203, \"name\" : \"Power Apparatus\", \"credits\" : 4}, {\"sno\" : \"TH4\", \"code\" : 204, \"name\" : \"Electrical Measurements\", \"credits\" : 4} ], \"core\" : [ {\"sno\" : \"TH1\", \"code\" : 201, \"name\" : \"Electronics I\", \"credits\" : 4}, {\"sno\" : \"TH2\", \"code\" : 202, \"name\" : \"Circuits and Systems\", \"credits\" : 4} ], \"theory\" : [ {\"sno\" : \"TH1\", \"code\" : 201, \"name\" : \"Electronics I\", \"credits\" : 4, \"category\" : \"C\"}, {\"sno\" : \"TH2\", \"code\" : 202, \"name\" : \"Circuits and Systems\", \"credits\" : 4, \"category\" : \"C\"}, {\"sno\" : \"TH3\", \"code\" : 203, \"name\" : \"Power Apparatus\", \"credits\" : 4, \"category\" : \"A\"}, {\"sno\" : \"TH4\", \"code\" : 204, \"name\" : \"Electrical Measurements\", \"credits\" : 4, \"category\" : \"A\"}, {\"sno\" : \"TH5\", \"code\" : 205, \"name\" : \"Mathematics III\", \"credits\" : 4, \"category\" : \"H\"} ], \"practical\" : [ {\"sno\" : \"PR1\", \"code\" : 206, \"name\" : \"Electronics I\", \"credits\" : 2}, {\"sno\" : \"PR2\", \"code\" : 207, \"name\" : \"Power Apparatus\", \"credits\" : 2}, {\"sno\" : \"PR3\", \"code\" : 208, \"name\" : \"Electrical Measurements\", \"credits\" : 2}, {\"sno\" : \"PR4\", \"code\" : 209, \"name\" : \"Machine Drawing\", \"credits\" : 3}, {\"sno\" : \"VS1\", \"code\" : 210, \"name\" : \"Programming I\", \"credits\" : 1} ] }, \"totalCredits\" : 30, \"semester\" : 3 }"
// use a function to directly retrieve a single subject
func getSubject(subject: String) -> [String: Any]? {
// use some guards to keep the nesting depth minimal
// read the json string
let data = jsonString.data(using: .utf8)
guard data != nil else {
return nil
}
// try to parse the json
let parsedJson = try? JSONSerialization.jsonObject(with: data!) as? [String: Any]
guard parsedJson != nil else {
return nil
}
// read the subjects
if let jsonDictionary = parsedJson! {
let subjects = jsonDictionary["subjects"] as? [String: Any]
guard subjects != nil else {
return nil
}
// get a single subject and interpret it as array
if let singleSubject = subjects![subject] as? [Any] {
// check if it contains elements
if singleSubject.count > 0 {
// return the first (and only) entry as dictionary
return singleSubject[0] as? [String: Any]
}
}
}
return nil
}
if var humanities = getSubject(subject: "humanities") {
// add a new entry
humanities["marks"] = 0
// print out all entries to check
for (key, value) in humanities {
print("\(key): \(value)")
}
}
印刷:
name: Mathematics III
marks: 0
sno: TH5
code: 205
credits: 4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.