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?