![](/img/trans.png)
[英]With a plist containing an array of arrays of dictionaries, how do you add and write out new dictionary elements
[英]Dictionary containing Arrays containing Dictionaries?
代碼段:
這來自一個稱為Static
的結構:
static var messages: Dictionary = [:]
這是在類函數內:
if Static.messages[sender] == nil{ //no message history, create array then append
var messages: [NSMutableDictionary] = [message]
Static.messages[sender] = messages
}
else{ //there is message history, so append
(Static.messages[sender] as Array).append(message)
}
錯誤:
Immutable value of type 'Array<T>' only has mutating members named 'append'
我正在嘗試制作一個會話詞典,每個項目都是一個人。 每個數組都是消息列表。 消息是字典類型。 知道我為什么收到此消息嗎?
如果您對編譯器很清楚字典中包含的內容,則不需要進行強制轉換,這會使此工作變得很困難。 從您發布的內容Static.messages
, Static.messages
的實際類型將需要類似於Dictionary<NSObject, Array<NSMutableDictionary>>
。
您當前的嘗試將字典值轉換為Array
,然后嘗試追加-失敗,因為Swift將這種轉換結果視為不可變。 您需要做的只是簡單地使用可選鏈接:
// instead of:
(Static.messages[sender] as Array).append(message)
// use:
Static.messages[sender]?.append(message)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.