0
votes

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")
    }
2
Can you show that what kind of url it is generating?Jigar Tarsariya
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.Hamish
@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/PHOTONAMEKerim
What's print("photo url: \(photoUrl)") printing?avismara
@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

2 Answers

2
votes

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()
            }
        }
    }
0
votes

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)