Swift3 JSON 错误 - 无法下标 AnyObject 类型的值
Swift3 JSON Error - Cannot subscript value of type AnyObject
在从 swift2 到 swift3 的转换中出现错误(无法下标类型 'AnyObject' 的值)。代码来自github用swift2.3
编写的大富翁游戏
let bundle = Bundle.main
let path = bundle.path(forResource: "properties", ofType: "json")
var error: NSError?
var data: NSData = NSData(contentsOfFile: path!)!
do {
let json = try? JSONSerialization.jsonObject(with: data as Data, options: JSONSerialization.ReadingOptions.allowFragments) as AnyObject
if let objects = json as? [AnyObject] {
for obj in objects {
var category = Category.special
switch obj["Group"] as String! { //error!
case "Brown": category = .brown
case "LightBlue": category = .lightBlue
case "Pink": category = .pink
case "Orange": category = .orange
case "Red": category = .red
case "Yellow": category = .yellow
case "Green": category = .green
case "Blue": category = .blue
default: category = .special
}
let name = obj["Name"] as String //error!
let price = obj["Price"] as Int //error!
let tile = obj["Position"] as Int //error!
var rent = 0
if let r = obj["Rent"] as? Int { //error! Ambiguous use of "subscript"
rent = r
}
let property = Property(name: name, category: category, price: price, tile: tile, rent: rent)
game.tiles.append(property)
}
}
} catch {
print("error")
}
JSON:
[
{
"Name":"GO",
"Position":1,
"Price":0,
"Rent":0,
"Group":"GO"
},
等等]
将您的 json 指定为像 [[String:Any]]
这样的字典数组,而不是将其声明为 [AnyObject]
。
if let objects = (try? JSONSerialization.jsonObject(with: data as Data, options: [])) as? [[String:Any]] {
for obj in objects {
var category = Category.special
switch obj["Group"] as! String {
case "Brown": category = .brown
case "LightBlue": category = .lightBlue
case "Pink": category = .pink
case "Orange": category = .orange
case "Red": category = .red
case "Yellow": category = .yellow
case "Green": category = .green
case "Blue": category = .blue
default: category = .special
}
//Add your other code
}
}
在从 swift2 到 swift3 的转换中出现错误(无法下标类型 'AnyObject' 的值)。代码来自github用swift2.3
编写的大富翁游戏 let bundle = Bundle.main
let path = bundle.path(forResource: "properties", ofType: "json")
var error: NSError?
var data: NSData = NSData(contentsOfFile: path!)!
do {
let json = try? JSONSerialization.jsonObject(with: data as Data, options: JSONSerialization.ReadingOptions.allowFragments) as AnyObject
if let objects = json as? [AnyObject] {
for obj in objects {
var category = Category.special
switch obj["Group"] as String! { //error!
case "Brown": category = .brown
case "LightBlue": category = .lightBlue
case "Pink": category = .pink
case "Orange": category = .orange
case "Red": category = .red
case "Yellow": category = .yellow
case "Green": category = .green
case "Blue": category = .blue
default: category = .special
}
let name = obj["Name"] as String //error!
let price = obj["Price"] as Int //error!
let tile = obj["Position"] as Int //error!
var rent = 0
if let r = obj["Rent"] as? Int { //error! Ambiguous use of "subscript"
rent = r
}
let property = Property(name: name, category: category, price: price, tile: tile, rent: rent)
game.tiles.append(property)
}
}
} catch {
print("error")
}
JSON: [ { "Name":"GO", "Position":1, "Price":0, "Rent":0, "Group":"GO" }, 等等]
将您的 json 指定为像 [[String:Any]]
这样的字典数组,而不是将其声明为 [AnyObject]
。
if let objects = (try? JSONSerialization.jsonObject(with: data as Data, options: [])) as? [[String:Any]] {
for obj in objects {
var category = Category.special
switch obj["Group"] as! String {
case "Brown": category = .brown
case "LightBlue": category = .lightBlue
case "Pink": category = .pink
case "Orange": category = .orange
case "Red": category = .red
case "Yellow": category = .yellow
case "Green": category = .green
case "Blue": category = .blue
default: category = .special
}
//Add your other code
}
}