我正在使用swift转换目标c代码。
我从服务器获取数据,其中包含词典中的词典。 我正在获取键值字符串,但无法获取字典。
示例数据:
data = {
caption = hello;
image = {
a = "https://www.google.com/1024x1024";
b = "https://www.google.com/640x640";
c = "https://www.google.com/480x480";
d = "https://www.google.com/";
};
};
我可以获得标题
let dataDict = (mainDict[data] as? Dictionary<String,AnyObject>)!
Obj.caption=String(dataDict["caption"]!) //getting hello
Obj.imageDictionary = (dataDict["image"] as? Dictionary<String,String>)! //getting 0 key value pairs
初始化imageDictionary为
var imageDictionary = Dictionary<String, String>()
请建议如何获取图像字典,我希望这个字典存储在imageDictionary对象中。
任何建议都将受到高度赞赏! 提前谢谢!
答案 0 :(得分:1)
试试这个:
let data : [String : Any] = ["caption" : "hello",
"image" :["a" : "https://www.google.com/1024x1024",
"b" : "https://www.google.com/640x640",
"c" : "https://www.google.com/480x480",
"d" : "https://www.google.com/",
]
]
let caption = data["caption"] as! String
let imageDictionary = data["image"] as! [String : String]
在imageDictionary
,我得到了:
["b": "https://www.google.com/640x640", "a": "https://www.google.com/1024x1024", "d": "https://www.google.com/", "c": "https://www.google.com/480x480"]