0

I getting nil error. But I didnt understand why happaned. I can get selectedPhoto name with print. But I cant use in NSUrl. Could you help me pls?

my codes:

    print(selectedPhoto)

    if selectedPhoto != nil
    {
        let photoUrl = NSURL(string: "http://www.kerimcaglar.com/uploads/yemek-resimler/\(selectedPhoto)")
        print("photo url: \(photoUrl)")
        dataPhoto = NSData(contentsOfURL:photoUrl!)
        yemekResim.image = UIImage(data: dataPhoto!)
    }

    else
    {
        print("Error")
    }
Kerim
  • 231
  • 1
  • 5
  • 15
  • Can you show that what kind of url it is generating? – Jigar Tarsariya May 17 '16 at 07:25
  • 1
    Print out the `dataPhoto` – that's probably what's returning `nil` if your `photoUrl` isn't. Although you really shouldn't be force unwrapping anyway. You should write the logic in order to deal with the case where either `photoUrl` or `dataPhoto` is `nil`, rather than just crossing your fingers and hoping they're not. Just because `selectedPhoto` exists, doesn't mean your url or data will. [See this Q&A for more info about why force unwrapping is bad](http://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu). – Hamish May 17 '16 at 07:36
  • @JigarTarsariya i have a table view. On it a meat name list when i will click open a new Page. Image meat photo and description about it. Http://www.kerimcaglar.com/yemek-tarifi this my json source. Meat photo www.kerimcaglar.com/yemek-resimler/PHOTONAME – Kerim May 17 '16 at 07:37
  • What's `print("photo url: \(photoUrl)")` printing? – avismara May 17 '16 at 07:38
  • @avismara nil. But I can write selectedPhoto. And I added Url . – Kerim May 17 '16 at 07:40
  • 1
    @Kerim On second thoughts, I bet the problem is due to the string interpolation. As `selectedPhoto` is an optional, your url string will be `"http://www....Optional("yourSelectedPhoto")"`. You need to unwrap `selectedPhoto` with an `if let` or `guard let`. – Hamish May 17 '16 at 07:41
  • @originaluser2 I don't understand solution :) sorry my English not good – Kerim May 17 '16 at 07:46

2 Answers2

2

From Apples documentation on NSData(contentsOfURL)

Do not use this synchronous method to request network-based URLs. For network-based URLs, this method can block the current thread for tens of seconds on a slow network, resulting in a poor user experience, and in iOS, may cause your app to be terminated.

If your app crashes because of this it will be rejected from the store.

Instead you should use NSURLSession. With the Async callback block as in my example. Also it is not a good idea to force unwrap optionals ! as you will get run time errors instead use the if let syntax

See my example below.

if let photo = selectedPhoto{
            let photoUrl = NSURL(string: "http://www.kerimcaglar.com/uploads/yemek-resimler/\(photo)")
            if let url = photoUrl{
                NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: {(data, response, error) in
                    if let d = data{
                        dispatch_async(dispatch_get_main_queue(), {
                            if let image = UIImage(data: d) {
                                self.yemekResim.image = image
                            }
                        })


                    }
                }).resume()
            }
        }
    }
Kent
  • 2,343
  • 13
  • 21
0

Replace this:

let photoUrl = NSURL(string: "http://www.kerimcaglar.com/uploads/yemek-resimler/\(selectedPhoto)")

with this:

let photoUrl = NSURL(string: "http://www.kerimcaglar.com/uploads/yemek-resimler/\(selectedPhoto!)")

(Notice the "!" after selectedPhoto)

Witterquick
  • 6,048
  • 3
  • 26
  • 50