如何使用Swift ioS在NSDictionary中附加数组?

问题描述 投票:-3回答:1

下面是我的字典:

    DATA:  [{
    carrier = "AT&T";
    email = "[email protected]";
    id = 18;
    phone = 3698521470;
}]

我想将新数组附加到此字典。这样,

DATA:  [{
    carrier = "AT&T";
    email = "[email protected]";
    id = 18;
    phone = 3698521470;
}, {
    carrier = "";
    email = "[email protected]";
    id = 19;
    phone = "";
}, {
    carrier = SPRINT;
    email = "";
    id = 20;
    phone = 3698521470;
}]

请帮助我!

swift append nsdictionary
1个回答
0
投票

*使用此功能

func toDictionary() -> NSDictionary {
        let blankDict : NSDictionary = [:]
        if let data = self.data(using: .utf8) {
            do {
                return try JSONSerialization.jsonObject(with: data, options: []) as! NSDictionary
            } catch {
                print(error.localizedDescription)
            }
        }
        return blankDict
    }


let dictInfo = responseData.toDictionary()
if let userArray = dictInfo["DATA"] as? [[String : Any]]{
   let newArray = userArray.append(yourNewArrayOfDictionary)
}
© www.soinside.com 2019 - 2024. All rights reserved.