Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Looping not working #64

Closed
vvrinne opened this issue Oct 8, 2014 · 3 comments
Closed

Looping not working #64

vvrinne opened this issue Oct 8, 2014 · 3 comments

Comments

@vvrinne
Copy link

vvrinne commented Oct 8, 2014

I am following the example for looping through a JSON response. Creating the initial JSON object works and I am able to fetch properties. When I try to loop through the root Dictionary XCode complains that: Type 'JSON' does not conform to protocol 'SequenceType'

I am using Alamofire to make the HTTP request. Here is my response handler:

request.responseJSON { (request, response, data, error) in
let json = JSON(object: data!)
for (key: String, subJson: JSON) in json {
println(subJson)
}
}

@LukeTangPL
Copy link
Member

@vvrinne try 2.0.0 not 1.1.0
The json which converted from NSData doing looping test at bc4a6fc

@vvrinne
Copy link
Author

vvrinne commented Oct 9, 2014

Yeah I am just using the master version. Isn't it a bit of a problem that the readme is not in sync with the code though?

I did eventually get looping to work in this fashion:

       if let dataArray  = json["data"].arrayValue {
            for entry: JSON in dataArray {
                let id = entry["id"]
                let title = entry["title"]
                println("\(id) : \(title)")
            }
        }

@LukeTangPL
Copy link
Member

@vvrinne You code will loop twice, one time to get arrayValue: [JSON], one time to loop the arrayValue to get each entry
In readme:

//If json is .Array
//The `index` is 0..<json.count's string value
for (index: String, subJson: JSON) in json {
//Do something you want
}

You should code like this:

for (_, entry) in json["data"] {
  let id = entry["id"]
  let title = entry["title"]
  println("\(id) : \(title)")
}

OR:

for var index = 0; index < json["data"].cout; index++ {
  let entry = json["data"][index]
  let id = entry["id"]
  let title = entry["title"]
  println("\(id) : \(title)")
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants