【发布时间】:2016-09-30 18:35:23
【问题描述】:
我已将 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
}
八个这样的字典存储在“主题”中。我想在“humanities”、“applied”等每个元素中添加另一个键值对“marks”: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,添加键/值对并将字典分配回数组。 -
@Prashant 我已添加代码
-
科目声明和学期仍然缺失。这些是解决您的问题的重要部分。请帮助我们帮助您。
-
我很抱歉。 “学期”与我遇到的问题无关。我添加了“主题”的声明