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")
}
dataPhoto
– that's probably what's returningnil
if yourphotoUrl
isn't. Although you really shouldn't be force unwrapping anyway. You should write the logic in order to deal with the case where eitherphotoUrl
ordataPhoto
isnil
, rather than just crossing your fingers and hoping they're not. Just becauseselectedPhoto
exists, doesn't mean your url or data will. See this Q&A for more info about why force unwrapping is bad. – Hamishprint("photo url: \(photoUrl)")
printing? – avismaraselectedPhoto
is an optional, your url string will be"http://www....Optional("yourSelectedPhoto")"
. You need to unwrapselectedPhoto
with anif let
orguard let
. – Hamish