林更新我的應用程序快捷3更新到SWIFT 3 JSON文件
我得到了幾個錯誤的
for (k, v): (AnyObject, AnyObject) in value {
獲取一個NSDictionary.Iterator.Element沒有可轉換到(Anyobject,Anyobject)
Subsiquently即時得到這個錯誤
var artworks = [Artwork]()
func loadInitialData() {
// 1
let fileName = Bundle.main.path(forResource: "PublicArt", ofType: "json");
let data: Data = try! Data(contentsOf: URL(fileURLWithPath: fileName!), options: NSData.ReadingOptions(rawValue: 0))
// 2
var error: NSError?
let jsonObject: AnyObject!
do {
jsonObject = try JSONSerialization.jsonObject(with: data,
options: JSONSerialization.ReadingOptions(rawValue: 0))
} catch let error1 as NSError {
error = error1
jsonObject = nil
}
// 3
if let jsonObject = jsonObject as? [String: AnyObject], error == nil,
// 4
let jsonData = JSONValue.fromObject(jsonObject)?["data"]?.array {
for artworkJSON in jsonData {
if let artworkJSON = artworkJSON.array,
// 5
let artwork = Artwork.fromJSON(artworkJSON) {
artworks.append(artwork)
}
}
}
}
的JSONObject產生 '任意' 不期望的內容相關的結果類型 'AnyObject'
和
參數類型[字符串:AnyObject]不符合預期類型 'AnyObject'
我假設這是一個容易的,但我沒有在一年內編碼,將非常讚賞的幫助
謝謝
特拉維斯
UPDATE
所以我剛剛更新的代碼
,但得到的JSON.swift錯誤文件
static func fromObject(_ object: AnyObject) -> JSONValue? {
switch object {
case let value as NSString:
return JSONValue.jsonString(value as String)
case let value as NSNumber:
return JSONValue.jsonNumber(value)
case _ as NSNull:
return JSONValue.jsonNull
case let value as NSDictionary:
var jsonObject: [String:JSONValue] = [:]
for (k, v): (AnyObject, AnyObject) in value {
if let k = k as? NSString {
if let v = JSONValue.fromObject(v) {
jsonObject[k as String] = v
} else {
return nil
}
}
}
return JSONValue.jsonObject(jsonObject)
case let value as NSArray:
var jsonArray: [JSONValue] = []
for v in value {
if let v = JSONValue.fromObject(v as AnyObject) {
jsonArray.append(v)
} else {
return nil
}
}
return JSONValue.jsonArray(jsonArray)
default:
return nil
}
}
}
錯誤是:
nsdictionary.iterate.element '(又名(標號:任意,值:任何)')不是 轉換爲 'AnyObject,AnyObject)'
對代碼行
for (k, v): (AnyObject, AnyObject) in value {
對不起已故的答覆
問候
特拉維斯
下半年的錯誤分別是// 2和// 4 – travis