Touch Tracker is building but not making lines

I am on the part where it says build and run and draw some lines. However, I can’t draw any lines. I imagine there is a spelling error but I can’t find it

//
// DrawView.swift
// TouchTracker
//
// Created by LARRY COMBS on 3/23/17.
// Copyright © 2017 LARRY COMBS. All rights reserved.
//

import UIKit

class DrawView: UIView {

var currentLine: Line?
var finishedLines = [Line]()

func stroke(_ line: Line) {
    let path = UIBezierPath()
    path.lineWidth = 10
    path.lineCapStyle = .round
    
    path.move(to: line.begin)
    path.addLine(to: line.end)
    path.stroke()
}

override func draw(_ rect: CGRect) {
    // Draw finished lines in black 
    UIColor.black.setStroke()
    for line in finishedLines {
        stroke(line)
    }
    
    if let line = currentLine {
        //If there is a line currently being drawn, do it in red
        UIColor.red.setStroke()
        stroke(line)
    }
}

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first!
    
    //Get location of the touch in view's coordinate system 
    let location = touch.location(in: self)
    
    currentLine = Line(begin: location, end: location)
    
    setNeedsLayout()
    
    }
    
    
    
override func touchesMoved(_ touches: Set<UITouch>, with: UIEvent?) {
    let touch = touches.first!
    let location = touch.location(in: self)
    
    currentLine?.end = location
    
    setNeedsLayout()
    
    }
    
func touchesEnded(_touches: Set<UITouch>, with: UIEvent?) {
    if var line = currentLine {
        let touch = _touches.first!
        let location = touch.location(in: self)
        line.end = location
        
        finishedLines.append(line)
    }
    currentLine = nil
    
    setNeedsLayout()
    
    }

}

The function’s signature looks broken a bit.

Also, the book should have asked you to override the function:

override func touchesEnded (_touches: Set <UITouch>, with event: UIEvent?) {
...
}

Thank you for the assist however, same error.

  1. Replace all calls to setNeedsLayout with calls to setNeedsDisplay. I missed this in my previous response because I did not really put your code under the electron microscope then. :slight_smile:

  2. Make sure that you use DrawView as the Custom Class for the view controller’s view.

The following code works.

//
//  DrawView.swift
//  TouchTracker
//
//  Created by ibex10 on 7/9/17.
//  Copyright © 2017 Swift Matters. All rights reserved.
//

import UIKit

struct Line {
    var begin : CGPoint
    var end   : CGPoint
}

class DrawView: UIView {
    
    var currentLine: Line?
    var finishedLines = [Line]()
    
    func stroke (_ line: Line) {
        let path = UIBezierPath()
        path.lineWidth = 10
        path.lineCapStyle = .round
        
        path.move(to: line.begin)
        path.addLine(to: line.end)
        path.stroke()
    }
    
    override func draw (_ rect: CGRect) {
        
        print ("\(type (of:self)): \(#function)")
        
        // Draw finished lines in black
        UIColor.black.setStroke()
        for line in finishedLines {
            stroke (line)
        }
        
        if let line = currentLine {
            //If there is a line currently being drawn, do it in red
            UIColor.red.setStroke()
            stroke(line)
        }
    }
    
    override func touchesBegan (_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first!
        
        //Get location of the touch in view's coordinate system
        let location = touch.location(in: self)
        
        currentLine = Line (begin: location, end: location)
        
        setNeedsDisplay()
        
    }
    
    override func touchesMoved (_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first!
        let location = touch.location(in: self)
        
        currentLine?.end = location
        
        setNeedsDisplay()
    }
    
    override func touchesEnded (_ touches: Set<UITouch>, with event: UIEvent?) {
        if var line = currentLine {
            let touch = touches.first!
            let location = touch.location(in: self)
            line.end = location
            
            finishedLines.append(line)
        }
        currentLine = nil
        
        setNeedsDisplay()
    }
}

Thank you so MUCH!!! it works with setNeedsDisplay()