类型 'AnyObject' 不符合协议 'sequenceType'
Type 'AnyObject' does not conform protocol 'sequenceType'
所以这是我的代码,我在以下行中收到错误,说 "Type 'AnyObject' does not conform protocol 'sequenceType'":
for bar in allBars {
我尝试了此处提出的解决方案 (Type 'AnyObject' does not conform to protocol 'SequenceType'),但它导致 Xcode 出现错误
我对 IOS 很陌生,所以这可能是一些愚蠢的东西
func loadBars(){
let path = NSBundle.mainBundle().pathForResource("donnes", ofType: "json")
let filename: String = "donnees.json"
do{
let data = try NSData(contentsOfFile: path!, options: NSDataReadingOptions.DataReadingMapped)
do{
let bars: AnyObject? = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions())
if let bars = bars as? Dictionary<String, AnyObject> {
print(bars)
let allBars = bars["bars"]
for bar in allBars {
let nouveauBar = Bar(id: bar["id"], name: bar["name"], adress: bar["adress"], url: bar["url"], image_url: bar["image_url"], tags: bar["tags"], longitude: bar["longitude"], latitude: bar["latitude"])
bars += nouveauBar
}
} else {
print("Level file '\(filename)' is not valid JSON")
}
}catch {
print("Level file '\(filename)' is not valid JSON: \(error)")
}
}catch {
print("Could not load level file: \(filename), error: \(error)")
}
}
这里是 .json 文件的开头:
{
"code": 200,
"bars": [{
"id": 1627,
"address": "100 Rue Saint-Martin, 75004 Paris, France, Opera and Les Halles, Paris",
"name": "Caf\u00e9 Beaubourg",
"url": "/bar/paris/opera-and-les-halles/cafe-beaubourg",
"image_url": "https://prh-wbb-prod.s3.amazonaws.com/generic_640_480/WBB%20holding%20image3_54abcf7d218d1_55b8d2128eabb.jpg",
"tags": "Bar, Gay, Cafe, Dinner, Lunch",
"latitude": 48.860033826521,
"longitude": 2.3508920416641
}, {
"id": 1259,
"address": "19 Rue Beaubourg, 75004 Paris, France, Bastille and the Marais, Paris",
"name": "Georges",
"url": "/bar/paris/bastille-and-the-marais/georges",
"image_url": "https://prh-wbb-prod.s3.amazonaws.com/generic_640_480/WBB%20holding%20image4_54abcf6d1f68d_55b8d2015bb13.jpg",
"tags": "Serves Food, Great View, Rooftop, Terrace, Rock",
"latitude": 48.8603,
"longitude": 2.35097
}]
}
而不是
if let bars = bars as? Dictionary<String, AnyObject> {
你可以写
if let bars = bars as? [String: AnyObject],
let allBars = bars["bars"] as? [[String: AnyObject]] {
这已经为您提供了 allBars
数组。然后编译器知道 allBars
中的每个 bar
将具有类型 [String: AnyObject]
(Dictionary<String, AnyObject>
的缩写),这允许您将 bar
用作字典。您仍然需要解包 id
和 name
等属性,您可以像我上面的代码那样执行此操作。
所以这是我的代码,我在以下行中收到错误,说 "Type 'AnyObject' does not conform protocol 'sequenceType'":
for bar in allBars {
我尝试了此处提出的解决方案 (Type 'AnyObject' does not conform to protocol 'SequenceType'),但它导致 Xcode 出现错误
我对 IOS 很陌生,所以这可能是一些愚蠢的东西
func loadBars(){
let path = NSBundle.mainBundle().pathForResource("donnes", ofType: "json")
let filename: String = "donnees.json"
do{
let data = try NSData(contentsOfFile: path!, options: NSDataReadingOptions.DataReadingMapped)
do{
let bars: AnyObject? = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions())
if let bars = bars as? Dictionary<String, AnyObject> {
print(bars)
let allBars = bars["bars"]
for bar in allBars {
let nouveauBar = Bar(id: bar["id"], name: bar["name"], adress: bar["adress"], url: bar["url"], image_url: bar["image_url"], tags: bar["tags"], longitude: bar["longitude"], latitude: bar["latitude"])
bars += nouveauBar
}
} else {
print("Level file '\(filename)' is not valid JSON")
}
}catch {
print("Level file '\(filename)' is not valid JSON: \(error)")
}
}catch {
print("Could not load level file: \(filename), error: \(error)")
}
}
这里是 .json 文件的开头:
{
"code": 200,
"bars": [{
"id": 1627,
"address": "100 Rue Saint-Martin, 75004 Paris, France, Opera and Les Halles, Paris",
"name": "Caf\u00e9 Beaubourg",
"url": "/bar/paris/opera-and-les-halles/cafe-beaubourg",
"image_url": "https://prh-wbb-prod.s3.amazonaws.com/generic_640_480/WBB%20holding%20image3_54abcf7d218d1_55b8d2128eabb.jpg",
"tags": "Bar, Gay, Cafe, Dinner, Lunch",
"latitude": 48.860033826521,
"longitude": 2.3508920416641
}, {
"id": 1259,
"address": "19 Rue Beaubourg, 75004 Paris, France, Bastille and the Marais, Paris",
"name": "Georges",
"url": "/bar/paris/bastille-and-the-marais/georges",
"image_url": "https://prh-wbb-prod.s3.amazonaws.com/generic_640_480/WBB%20holding%20image4_54abcf6d1f68d_55b8d2015bb13.jpg",
"tags": "Serves Food, Great View, Rooftop, Terrace, Rock",
"latitude": 48.8603,
"longitude": 2.35097
}]
}
而不是
if let bars = bars as? Dictionary<String, AnyObject> {
你可以写
if let bars = bars as? [String: AnyObject],
let allBars = bars["bars"] as? [[String: AnyObject]] {
这已经为您提供了 allBars
数组。然后编译器知道 allBars
中的每个 bar
将具有类型 [String: AnyObject]
(Dictionary<String, AnyObject>
的缩写),这允许您将 bar
用作字典。您仍然需要解包 id
和 name
等属性,您可以像我上面的代码那样执行此操作。