我有这个Swift代码,我试图在其中添加字典到数组。

        var savedFiles: [Dictionary<String, AnyObject>] = []
        var newEntry =  Dictionary<String,AnyObject>()

        if let audio = receivedAudio?.filePathURL {
            newEntry["url"] = audio
        }
        newEntry["name"] = caption

        savedFiles.append(newEntry! as Dictionary<String,AnyObject>)


这使我在最后一行(附加)Cannot invoke 'append' with an argument list of type '(Dictionary<String, AnyObject>)'上出现错误

任何想法?我还尝试了消除力量展开。

最佳答案

请尝试以下方法:

var savedFiles: [[String: AnyObject]] = []
var newEntry: [String: AnyObject] = [:]

if let audio = receivedAudio?.filePathURL {
    newEntry["url"] = audio
}
newEntry["name"] = caption

savedFiles.append(newEntry)

关于ios - 将字典追加到Swift数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30341549/

10-09 02:39