Renaud
Renaud

Reputation: 61

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'":

for bar in allBars {

I tried the solution proposed here (Type 'AnyObject' does not conform to protocol 'SequenceType') but it causes Xcode to bug

I'm very new on IOS so this might be some stupid stuff

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:

{
"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
}]

}

Upvotes: 0

Views: 2566

Answers (1)

Tim Vermeulen
Tim Vermeulen

Reputation: 12562

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. 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. You still have to unwrap the properties such as id and name, you can do that similarly to my code above.

Upvotes: 1

Related Questions