asynchronous - Code after NSURLSession runs before session is over - Swift -


i have app accesses website , downloads html site, converts string, grabs information string, , uses app. problem code extracting string runs before data can loaded. tried putting code in completion handler, either didn't fix problem, or did wrong. here code:

for var = 0; <= textviewcontentarray.count - 1; i++ {     var dontcount = false     istaskcomplete = false     var currentword = textviewcontentarray[i]             let url = nsurl(string: "http://words.bighugelabs.com/" + currentword)              var request: nsurlrequest = nsurlrequest(url: url!)             let config = nsurlsessionconfiguration.defaultsessionconfiguration()             let session = nsurlsession(configuration: config)              let task : nsurlsessiondatatask = session.datataskwithrequest(request, completionhandler: {(data, response, error) in                  if error == nil {                     //getting string url data                     let urlcontent = nsstring(data: data!, encoding: nsutf8stringencoding)!                      if urlcontent.containsstring("word not found.") == false && urlcontent.containsstring("i couldn't find that.") == false && urlcontent.containsstring("not found") == false {                         //extracting needed info string                         let startpointarray = urlcontent.componentsseparatedbystring("ul class=\"words\">")                          let url1 = startpointarray[1]                          let endpointarray = url1.componentsseparatedbystring("</ul><h3>sounds kind of like")                         var wordsbodystring = endpointarray[0]                          wordsbodystring = wordsbodystring.stringbyreplacingoccurrencesofstring("</a></li>", withstring: "")                         let wordsbasicarray = wordsbodystring.componentsseparatedbystring("<li><a href=\"/")                          var wordsadvancedarray: [string] = []                          var = 0; <= wordsbasicarray.count - 1; i++ {                             var currentwordarray = wordsbasicarray[i].componentsseparatedbystring("\">")                              wordsadvancedarray.append(currentwordarray[0] as! string)                          }                          var = 0; <= wordsadvancedarray.count - 1; i++ {                             if count(wordsadvancedarray[i]) < count(currentword) {                                 shorterwordsarray.append(wordsadvancedarray[i])                             }                         }                          shorterwordsarray.removeatindex(0)                          shorterwordsarrayglobal[i] = shorterwordsarray                          })                         task.resume() } 

the urlcontent constant assigned value before website data loaded. need way stop happening. new programing, keep in mind answer, thank in advance.


Comments

Popular posts from this blog

renaming files in a directory using python or R -

c# - ajax - How to receive data both html and json from server? -