1
votes
let imgURL:NSURL = NSURL(string: "\(ImageName)")!

at the above line,i'm getting fatal error

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

Code :

let ImageName = obj["image"] as! String

 let imgURL:NSURL = NSURL(string: "\(ImageName)")!
 let request: NSURLRequest = NSURLRequest(URL: imgURL)

  let session = NSURLSession.sharedSession()

  let Imgtask = session.dataTaskWithRequest(request){
                            (data, response, error) -> Void in

   if (error == nil && data != nil)
    {
       func display_image()
       {
           pointAnnoation.DisplayImage = UIImage(data: data!)
        }
        dispatch_async(dispatch_get_main_queue(), display_image)
     }

   }

   Imgtask.resume()

From the above code im trying to store my image from database in annotation

if i printed the 'ImageName' it returns the name from the database correctly, but unable to retain the image

it resulting in the error while running.

4
Please consider to use the recommended naming convention: functions, methods and variables/properties start with a lowercase letter, types like classes, structs, enums and protocols with an uppercase letter.vadian
If you put '!' on anything that can be nil, you're asking for a crash. Try testing the value instead. (What is the value of ImageName when you print it?)Phillip Mills
its a shorturl from database (like goo.gl/pBmA0d)Sridhar G.K

4 Answers

3
votes

You say that

if i printed the 'ImageName' it returns the name from the database correctly

Then that must mean that the ImageName is not valid for a URL

If you look at the description of NSURL(string:) it says:

The URL string with which to initialize the NSURL object. This URL string must conform to URL format as described in RFC 2396, and must not be nil. This method parses URLString according to RFCs 1738 and 1808.

So the question is...how does ImageName look? And can you create a URL from it?

Apart from that, it is always a good idea to use ? instead of ! as @PhillipMills says

Update: I can see that you have posted an example of your URL now. If I do this in a playground:

let url = NSURL(string: " goo.gl/pBmA0d")

I get nil in return, so it would seem that short URLs and NSURLaren't the best of friends.

Update 2: hmm, guess I spoke to quickly, if you look at the above you can see that I have a space before the goo.gl part, if I change that to:

let url = NSURL(string: "goo.gl/pBmA0d")

it actually works, I get a NSURL object.

But another thing I stumbled upon in your code. You declare ImageName as a String here:

let ImageName = obj["image"] as! String

So you don't have to wrap it in \() later on

let imgURL:NSURL = NSURL(string: "\(ImageName)")!

You could simply say:

let imageURL = NSURL(string: ImageName)

And then...as others has said, it is always a good idea to use ? instead of !

So you could write:

if let imageName = obj["image"] as? String,
   let imageURL = NSURL(string: imageName) {
   //we're in business :-)
}

and be safe and sound

2
votes

Try to use guard or if let for helping yourself.

let ImageName = obj["image"] as! String

 if let imgURL = NSURL(string: ImageName) {
     let request: NSURLRequest = NSURLRequest(URL: imgURL)

     let session = NSURLSession.sharedSession()

     let Imgtask = session.dataTaskWithRequest(request){ (data, response, error) -> Void in

        if (error == nil && data != nil)
        {
            // What's that func??
            func display_image()
            {
                pointAnnoation.DisplayImage = UIImage(data: data!)
            }
            dispatch_async(dispatch_get_main_queue(), display_image)
         }

       }
 }
 Imgtask.resume()
0
votes

Don't make force unwrap...use if let to avoid crash ...

 if let img = obj["image"] as? String, 
      imgURL = NSURL(string: img) {

     // ... continue with your code ...
 }
0
votes

Please try the following code:

    //ImageName is a String type.

    guard  let ImageName = obj["image"] as? String , let imgURL = NSURL(string: ImageName) else{
        return 
    }
    let request: NSURLRequest = NSURLRequest(URL:imgURL)

    let session = NSURLSession.sharedSession()

    let Imgtask = session.dataTaskWithRequest(request){
        (data, response, error) -> Void in

        if (error == nil && data != nil)
        {
            func display_image()
            {
                pointAnnoation.DisplayImage = UIImage(data: data!)
            }
            dispatch_async(dispatch_get_main_queue(), display_image)
        }

    Imgtask.resume()