我是一个非常新的快速并花了一整天试图解析简单的json,接受了思想的websockets。最后我解析了(有点),但很多事情仍然超出了我的理解,我要求一些解释。
好的,这是我到目前为止所得到的
首先,我声明变量QUOTES,它将是Dictionary,与json
相同var QUOTES: Dictionary<String, Dictionary<String, Any>>
我希望我的QUOTES var在最后看起来像这样:
{
"AAPL": {name: "Apple Comp", "price": 100},
"F": {name: "Ford Comp", "price": 200}
}
当我的websocket接收数据时,我想用接收到的数据填充QUOTES。
socket.on("q"){data, ack in
//data looks like this: [{q:[{c: "AAPL", price: 100}, {c: "F", price: 100}]}]
//I convert first element of received [Any] to dictionary
let object = data[0] as! Dictionary<String, NSArray>
//I get array of quotes out of received data
let quotes = object["q"] as! [Dictionary<String, Any>]
//Now I want to iterate through received quotes to fill me QUOTES variable declared in the very beginning
for quote in quotes {
//I know ticker of current quote
let ticker = quote["c"] as! String
//Not sure if I have to do it - if QUOTES does not have current ticker in dictionary, I create it as empty dictionary
if (QUOTES[ticker] == nil) {
QUOTES[ticker] = [String: Any]()
}
//Now I iterate properties of received quote
for(k,v) in quote {
//And I want to fill my QUOTES dictionary,
// but I get compile error
//Value of optional type '[String : Any]?' not unwrapped; did you mean to use '!' or '?'?
//I don't understand what compiler wants me to do?
QUOTES[ticker][k] = v
}
}
}
但我收到编译错误
可选类型的值&#39; [字符串:任意]?&#39;没有打开;你的意思是使用&#39;!&#39;或者&#39;?&#39;?
我不明白编译器要我做什么?