简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM