これが私のコードです。次の行に「タイプ 'AnyObject' はプロトコル 'sequenceType' に準拠していません」というエラーが表示されます。
for bar in allBars {
ここで提案された解決策を試しました ( Type 'AnyObject' はプロトコル '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
}]
}