[英]Type 'AnyObject' does not conform protocol 'sequenceType'
So this is my code, I get an error in the following line, saying "Type 'AnyObject' does not conform protocol 'sequenceType'": 因此,这是我的代码,在以下行中出现错误,提示“ Type'AnyObject'不符合协议'sequenceType'”:
for bar in allBars {
I tried the solution proposed here ( Type 'AnyObject' does not conform to protocol 'SequenceType' ) but it causes Xcode to bug 我尝试了此处提出的解决方案( 类型“ AnyObject”不符合协议“ SequenceType” ),但它导致Xcode出现错误
I'm very new on IOS so this might be some stupid stuff 我在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)")
}
}
And here is the beginning of the .json file: 这是.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
}]
} }
Instead of 代替
if let bars = bars as? Dictionary<String, AnyObject> {
you could write 你可以写
if let bars = bars as? [String: AnyObject],
let allBars = bars["bars"] as? [[String: AnyObject]] {
This already gives you the allBars
array. 这已经为您提供了
allBars
数组。 Then the compiler knows that each bar
in allBars
will have type [String: AnyObject]
(short for Dictionary<String, AnyObject>
) which allows you to use bar
as a dictionary. 然后,编译器知道
allBars
中的每个bar
将具有[String: AnyObject]
类型( Dictionary<String, AnyObject>
),该类型允许您将bar
用作字典。 You still have to unwrap the properties such as id
and name
, you can do that similarly to my code above. 您仍然必须解开
id
和name
类的属性,您可以执行与上述代码类似的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.