-4

I have a Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value error in my project I tray so many but I can't fix it. anyone can help me. enter image description here this is the code: I think the problem is with the class (canvas) because when I call the view it will be a problem but my application entails this.

    import UIKit
import Foundation
class WarmUpControlClass: UIViewController {

    @IBOutlet weak var UserImage: UIImageView!
    let canvas = WarmUp()
    //func to creates and instantiates the UIView object (warmUp class object)
   override func loadView() {
 //to call functions in the warm up class to enable user drawing

   self.view = canvas

    }
    override func viewDidLoad() {
         //to show the user image in the top right corner

              let decodedData = Data(base64Encoded: SelectedUser.ImageString, options: .ignoreUnknownCharacters)
              let decodedimage:UIImage? = UIImage(data: decodedData! as Data)
              UserImage.image = decodedimage //i put .image to convert from UIimage to UIimageVeiw
              UserImage.image = decodedimage
              UserImage.layer.cornerRadius = UserImage.frame.size.width/2
              UserImage.clipsToBounds = true


        canvas.backgroundColor = .white //to set white background to drawing screen
        super.viewDidLoad()
}//end conterol class
    //this class to do all drawing methods (user writing)
    class WarmUp: UIView {



        override func draw(_ rect: CGRect) {
               super.draw(rect)  //custom drawing
               guard let context = UIGraphicsGetCurrentContext() else
               { return }
            context.setStrokeColor(UIColor.gray.cgColor)
               context.setLineWidth(8)
               context.setLineCap(.round)
               lines.forEach { (line) in
                       for(i , p) in line.enumerated(){
                           if i == 0 {
                           context.move(to: p)
                           } else {
                           context.addLine(to: p)
                           }
                       }//end for
                    }
               context.strokePath()
           }
           var lines = [[CGPoint]]()//two dimantional array
           override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
               lines.append([CGPoint]())
           }
           //track the finger as we move across screen
           override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
               guard let point = touches.first?.location(in: nil) else {return}

               guard var lastLine = lines.popLast() else { return }
               lastLine.append(point)

               lines.append(lastLine)

               setNeedsDisplay()
           }

    }

}

Does anyone help?

Sarah
  • 5
  • 5
  • The `UserImage` outlet isn't connected. See the duplicate for details. And please, always post code as text, not as a picture. – rmaddy Oct 05 '19 at 16:28
  • I apologize very much for that, but I made sure he is really connected – Sarah Oct 05 '19 at 16:32
  • Then see this one: https://stackoverflow.com/questions/29321383/iboutlet-is-nil-but-it-is-connected-in-storyboard-swift – rmaddy Oct 05 '19 at 16:45
  • Without more information in your question, there is no way to provide any further assistance. – rmaddy Oct 05 '19 at 17:16
  • I retrieve an image from database I decoded it to display it in (UserImage) also in the same view I have class (canvas) for the draw – Sarah Oct 06 '19 at 06:22
  • @rmaddy I but all code can you help me, please :( – Sarah Oct 06 '19 at 16:21

1 Answers1

-2

DecodedImage is nil. You should check, why it is nil.

It is always better to ask wether an optional is nil e.g.

If let image = imageOptional {
// in here image is not optional
} else {
// maybe print error here
}
Chris
  • 7,579
  • 3
  • 18
  • 38