[英](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.