-1

My app is crashing when it gets to this block.

let uploadFile:PFFile = tweet["uploadedPic"] as PFFile
uploadFile.getDataInBackgroundWithBlock {
    (uploadData:NSData!, error:NSError!)-> Void in
     let uploadImage:UIImage = UIImage(data: uploadData)!
     cell.attachedImage.image = uploadImage
}

Why would I be receiving this error?

fatal error: unexpectedly found nil while unwrapping an Optional value

Any ideas?

Thanks!

  • 1
    This questions pops up at least 5 times a day on SO. – zisoft Nov 24 '14 at 08:31
  • Attempted to improve your question (it's a ***low quality question***) but couldn't, so I've flagged as VLQ. Please add more details such as the code before the crash happens. – AStopher Nov 24 '14 at 08:32
  • @cybermonkey what do you mean? I have narrowed it down to this block of code everything else is fine :) – user3679109 Nov 24 '14 at 08:33
  • In which line you got this error? – Dharmesh Kheni Nov 24 '14 at 08:36
  • @user3679109 You obviously haven't done any research, as well as this you haven't explained ***what your app does*** and *what the code **does** that you have included*. – AStopher Nov 24 '14 at 08:36
  • @DharmeshKheni the crash shows on the first line but I don't think that is it – user3679109 Nov 24 '14 at 08:38
  • Have you set a breakpoint or added println() statements to isolate your problem? Have you checked if `uploadData` is nil? Have you checked if `error` is set? Have you checked if `UIImage(data: uploadData)` (without the exclamation mark!) returns nil? – Martin R Nov 24 '14 at 08:40
  • @cybermonkey it is a social network and this block is finding the image that the user uploaded to the server and applying it to a UIImageView – user3679109 Nov 24 '14 at 08:40
  • 1
    If you try doing [a Google search](https://www.google.com/?gws_rd=ssl#q=unexpectedly+found+nil+while+unwrapping+an+Optional+value), you'll see that the first 5 results are your question. The first 3 of those links are on Stack Overflow. If none of those answers solve your problem, try explaining how your problem is different. –  Nov 24 '14 at 09:06

1 Answers1

0

Try this way:

if let uploadImage = UIImage(data: uploadData)!{
 cell.attachedImage.image = uploadImage
}

This error occurs because your uploadData is become nil at run time so check your code and find out yourself that why uploadData is become nil at run time.

This code will not give you any error if your uploadData will be nil.

EDIT:

If you get this error on first line then you can do like this way :

if let uploadFile = tweet["uploadedPic"] as? PFFile {
// Your code
 }
Dharmesh Kheni
  • 67,254
  • 32
  • 154
  • 160
  • Thanks for the answer, I had an error in the if statement where you had the ! and I changed it to ? and the error was gone. The app crash is still occurring and showing on the same line. Anything else it could be? – user3679109 Nov 24 '14 at 08:42
  • I have update the answer. let me know if it helps you – Dharmesh Kheni Nov 24 '14 at 08:43
  • Thank you very much! It is fixed now, have approved the answer :) – user3679109 Nov 24 '14 at 08:45
  • happy to help you.. and you can refer this question to make your code more explicit.http://stackoverflow.com/questions/25948401/unexpectedly-found-nil-while-unwrapping-an-optional-value-when-retriveing-pffi – Dharmesh Kheni Nov 24 '14 at 08:47