James Gifford
James Gifford

Reputation: 53

How can I obtain data from consecutive URLs

I have consecutive Url's up to page number when I try to execute in a loop it does not work here is my code I want to get data from different URLs up to page number

for(var i = 1 ; i < 50; i ++)
{
    var pageNumber = 1
    var urlPath = "https://api.blabla.com/blabla?page=\(pageNumber)"
    var url = NSURL(string: urlPath)
    var task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { data,response,error  -> Void in
        if(error == nil)
        {
            //JSON Parse
        }
    })
    task.resume()
    pageNumber++
}

When I do this it works for pageNumber = 1 but the problem is that it gives the same Url each time, basicly when I write println(url!) in //JSON Parse. Every time it gives me:

api.blabla.com.blabla?page=1

So I checked that data does not nil but it only works for pagenumber = 1

Upvotes: 0

Views: 51

Answers (1)

Peter Foti
Peter Foti

Reputation: 5654

You're setting pageNumber to 1 every time the loop starts anew. Get rid of that variable altogether and just use i.

Upvotes: 2

Related Questions