Look at this code:
let url = NSURL(string: physicst.image as String) if let url = url { let request = NSMutableURLRequest(URL: url) let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in if let data = data { cell.imageView?.image = UIImage(data: data) } }) task.resume() }else { print ("nill URL \(physicst.image)") } so I have a string, and that string is a url, and I want to load it.
that code is in a table view cells, so it is being called for each cell.
as you see, i am checking if the url is nil or not, and if not, i am making a print statement. almost all the urls are not nil, exception the following ones (which are complemetly valid)
http://commons.wikimedia.org/wiki/Special:FilePath/Kai_Manne_Börje_Siegbahn.jpg?width=300
http://commons.wikimedia.org/wiki/Special:FilePath/Виталий_Лазаревич_Гинзбург.jpg?width=300
http://commons.wikimedia.org/wiki/Special:FilePath/赤崎記念研究館.jpg?width=300
http://commons.wikimedia.org/wiki/Special:FilePath/Kai_Manne_Börje_Siegbahn.jpg?width=300
http://commons.wikimedia.org/wiki/Special:FilePath/赤崎記念研究館.jpg?width=300
The first thing you may argue is to encode the url, and that is what I did like this:
var image = oneBinding["p"]!!["value"] as! NSString image = image.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())! but then even the urls that were working, stopped working. what i am missing please ?
Update
Here is the whole code of my UITableViewController (it is easy)
class PhysicistTableViewController: UITableViewController { var physicsts : [Physicst]? @IBOutlet var physicstsTableView: UITableView! override func viewDidLoad() { super.viewDidLoad() loadDataFromDBPedia() } func loadDataFromDBPedia() { let session = NSURLSession.sharedSession() var url = "http://dbpedia.org/sparql/" let query = "http://dbpedia.org&query=select * {?o dbo:thumbnail ?p . ?o dbo:award dbr:Nobel_Prize_in_Physics}" url = url + "?default-graph-uri=" + query.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())! url = url + "&format=JSON&CXML_redir_for_subjs=121&CXML_redir_for_hrefs=&timeout=30000&debug=on" let request = NSMutableURLRequest(URL: NSURL(string: url)!) let task = session.dataTaskWithRequest(request, completionHandler: {(data, response ,error) in if let error = error { print ("\(error)") } if let response = response { let httpResponse = response as! NSHTTPURLResponse let statusCode = httpResponse.statusCode print("Status code = \(statusCode)") } if let data = data { do { let jsonResponse = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions()) let binding = jsonResponse["results"]!!["bindings"] as! NSArray for oneBinding in binding { let name = oneBinding["o"]!!["value"] as! NSString let image = oneBinding["p"]!!["value"] as! NSString //image = image.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())! let physicst = Physicst(image: image, name: name) if self.physicsts == nil { self.physicsts = [Physicst]() } self.physicsts!.append(physicst) } self.physicstsTableView.reloadData() }catch _ { print ("not well json-formatted response") } } }) task.resume() } override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if self.physicsts == nil { return 0 }else { return self.physicsts!.count } } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("physicstCell")! as UITableViewCell let row = indexPath.row let physicst = self.physicsts![row] cell.textLabel?.text = physicst.name as String if (physicst.imageData == nil) { let session = NSURLSession.sharedSession() let url = NSURL(string: physicst.image as String) if let url = url { let request = NSMutableURLRequest(URL: url) let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in if let data = data { let imageData = UIImage(data: data) cell.imageView?.image = imageData physicst.imageData = imageData self.physicstsTableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None) } }) task.resume() }else { print ("nill URL \(physicst.image)") } }else { cell.imageView?.image = physicst.imageData! } return cell } }