我正在尝试解析JSON Response,创建模型的实例并填充tableview。
这是我的答复:
{
"jsonrpc": "2.0",
"id": 1,
"result": [
{
"token": "4L12XJHPWM18",
"name": "Berlin",
"nameShort": "BLN",
"country": "Germany",
"mapLat": 52.5078884,
"mapLng": 13.4392219,
"mapZoom": 13,
"timezone": "CET"
},
...
]
}
这是我获取它并解析它的方式
Alamofire.request(.POST, BGAPIBaseUrlV1, parameters: JSONObject, encoding: .JSON)
.responseJSON { (request: NSURLRequest, response: NSHTTPURLResponse?, object: AnyObject?, error: NSError?) -> Void in
if (object != nil) {
var responseDic = object as [String: AnyObject]
var result: AnyObject? = responseDic["result"]
if let resultArray = result as? Array<Dictionary<String, AnyObject>> {
if (success != nil) {
success!(resultArray)
}
}
}
if (error != nil) {
failure!(error!)
}
}
没问题,直到我尝试循环传递给闭包的数组
APIManager.fetchCities({ (cities: Array<Dictionary<String, AnyObject>>) -> Void in
for tmpCity in cities {
var city: CityModel = CityModel(dict: tmpCity) <----------
self.tableViewData.append(city)
}
self.tableView.reloadData()
}, failure: { (error: NSError) -> Void in
println("It didn't work")
})
我得到了EXC_BAD_INSTRUCTION以及以下错误
致命错误:不能在不同大小的类型之间使用unsafeBitCast
知道为什么吗?
答案 0 :(得分:3)
可能您在&#34; ...&#34;中替换String!
类型的内容,例如如果使用,您将收到错误:
var str: String! = ""
var data: NSDictionary = [ "myString": str ]
但是
var str: String = ""
var data: NSDictionary = [ "myString": str ]
会正常工作。