Kerim Kerim - 1 year ago 77
Swift Question

Fatal Error: Unexpectedly found nil while unwrapping an Optional value NSURL

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:


if selectedPhoto != nil
let photoUrl = NSURL(string: "\(selectedPhoto)")
print("photo url: \(photoUrl)")
dataPhoto = NSData(contentsOfURL:photoUrl!)
yemekResim.image = UIImage(data: dataPhoto!)


Answer Source

Replace this:

let photoUrl = NSURL(string: "\(selectedPhoto)")

with this:

let photoUrl = NSURL(string: "\(selectedPhoto!)")

(Notice the "!" after selectedPhoto)