Quick fatal error: Null found unexpectedly while expanding optional uiimage value

I have an error and UIImage is assigning an image of a property to the UIImageView

func connectionDidFinishLoading(connection: NSURLConnection!) {
  var err: NSError
  var jsonResult : NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
  println("\(jsonResult)")
  if jsonResult.count > 0 {
    let homepage = jsonResult.objectForKey("homepage") as NSMutableDictionary
    if homepage.count > 0 {
      let imageUrl = homepage.objectForKey("url_img") as NSString
      let backgroundImageUrl = NSURL.URLWithString(imageUrl)
      let backgroundImageData = NSData(contentsOfURL: backgroundImageUrl)
      var image :UIImage = UIImage(data: backgroundImageData)
      println("\(image)")
      self.backgroundImage.image = image
    }
  }
}

      

This is the delegate function from the get request. backgroundImageData

and image

not zero, but it doesn't work, I don't understand.

I am a newbie with Swift: s

This code works with objective-C

NSString *imageUrl = [metaData objectForKey:@"url_img"];
NSURL *backgroundImageUrl = [NSURL URLWithString:imageUrl];
NSData *backgroundImageData = [NSData dataWithContentsOfURL:backgroundImageUrl];
self.backgroundImage.image = [UIImage imageWithData:backgroundImageData];

      

+3


source to share


1 answer


The data is good, the image is not zero. The error here is not in the code, but in where the request is being made. the request needs a delegate object, I thought I was giving it a controller, but it was not set by the controller, HomeController () à la place self My bad ^^ "



+1


source







All Articles