-2

I am new to coding and have been trying to make an area on the screen that I can sign with my finger. I have made the box but I am struggling to clear it. I have made a button connected to a function to clear the path but I can't seem to work out how to safely unwrap the information without it crashing.

import UIKit

class canvasView: UIView {

    var lineColour:UIColor!
    var lineWidth:CGFloat!
    var path:UIBezierPath!
    var touchPoint:CGPoint!
    var startingPoint:CGPoint!


    override func layoutSubviews() {
        self.clipsToBounds = true
        self.isMultipleTouchEnabled = false

        lineColour = UIColor.white
        lineWidth = 10
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first
        startingPoint = (touch?.location(in: self))!
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first
        touchPoint = touch?.location(in: self)

        path = UIBezierPath()
        path.move(to: startingPoint)
        path.addLine(to: touchPoint)
        startingPoint = touchPoint

        drawShapelayer()
    }
    func drawShapelayer(){
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = path.cgPath
        shapeLayer.strokeColor = lineColour.cgColor
        shapeLayer.lineWidth = lineWidth
        shapeLayer.fillColor = UIColor.clear.cgColor
        self.layer.addSublayer(shapeLayer)
        self.setNeedsDisplay()
    }

    func clearCanvas() {
        path.removeAllPoints()
        self.layer.sublayers = nil
        self.setNeedsDisplay()
    }

I then get the error in my final function after

path.removeAllPoints()

How is best to unwrap it to stop it crashing?

Thank you for your patience

atline
  • 28,355
  • 16
  • 77
  • 113
  • 1
    Possible duplicate of [What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?](https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – Kamran Nov 21 '18 at 05:08

1 Answers1

0

The problem is that if the user clicks the button to clear the canvas before he/she draws anything, then the error will occur, since path is only assigned a value in touchesMoved().

You might want to change

var path:UIBezierPath!

to

var path:UIBezierPath?

Though this may seem tedious since you have to add question marks everywhere you try to access a method or property of path, it's much safer, and the code in your example will not crash.

P.S. Check out this answer. It provides a lot of information regarding the use of optionals.

Xcoder
  • 1,433
  • 3
  • 17
  • 37
  • Thank you for that. it seems to remove one error and give me another. Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT). I will have to look at it again and try and work out where I am going wrong. – James Bethell Nov 21 '18 at 05:44
  • No problem. You can post your error in another question and I can have a look at it :) – Xcoder Nov 23 '18 at 15:55