i use this function to get the link of image but i just have the variable in the initialization.
func getLinkImage(link_news: String, separator: String) -> String {
let url = NSURL(string: link_news)
var link_image_news = "http://www.website.com"
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) { (data, response, error) -> Void in
if error == nil {
let urlContent = NSString(data: data!, encoding: NSUTF8StringEncoding)
//print(urlContent)
let urlContentArray = urlContent?.componentsSeparatedByString(separator)
// search link's image
print("----------Link's Image----------")
var image_news = urlContentArray?[1].componentsSeparatedByString("<img alt=")
image_news = image_news?[1].componentsSeparatedByString("src=")
image_news = image_news?[1].componentsSeparatedByString("\"")
link_image_news = "http://www.website.com" + image_news![1]
print("the link of image is : "+link_image_news)
// end of search link's image
}
else {
print("Error in the Image News load from Website")
print(url!)
}
}
task.resume()
return link_image_news
}
when i call the function, i have only the initialization value (link_image_news = http://www.website.com), after many seconds i have the print with right value (the link of image).
i think it's issue with response time of server. how can i solve this ? i found some stuffs with closure (completion) but i dont really understand how it's works, im new in Swift