farmerhnf
farmerhnf

Reputation: 13

Swift Error 'Use of Undeclared type'

I am just starting out worth swift so the answer is probably standing right in front of me, but unfortunately I can't see it.

I have got an error with the attached code that says 'Use of Undeclared type 'line''

So I wondered if you guys could point me in the right direction with a little bit of an explanation so I can learn from my mistakes. Here is the code:

import UIKit

class DrawView: UIView {

    var lines: [Line] = []
    var lastPoint: CGPoint!
    var drawColor = UIColor.blackColor()

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        self.lastPoint = (touches.anyObject() as UITouch).locationInView(self)
    }
    override func touchesMoved(touches: NSSet, withEvent event: UIEvent)  {
        var newPoint = (touches.anyObject() as UITouch).locationInView(self)
        self.lines.append(Line(start: self.lastPoint, end: newPoint, color: self.drawColor))
        self.lastPoint = newPoint
        self.setNeedsDisplay()
    }
    override func drawRect(rect: CGRect)  {
        var context = UIGraphicsGetCurrentContext()
        CGContextSetLineCap(context, kCGLineCapRound)
        CGContextSetLineWidth(context, 3)
        for line in self.lines {
            CGContextBeginPath(context)
            CGContextMoveToPoint(context, line.startX, line.startY)
            CGContextAddLineToPoint(context, line.endX, line.endY)
            CGContextSetStrokeColorWithColor(context, line.color.CGColor)
            CGContextStrokePath(context)
        }
    }
}

Upvotes: 0

Views: 2929

Answers (2)

Gabriel Bitencourt
Gabriel Bitencourt

Reputation: 487

This part of the code is giving you the error:

var lines: [Line] = []

It's happennig because Line is not a Swift class or type (or anything, actually), so it can't be in an Array. For Line to be known by swift you gotta make a class for it. Like this:

class Line{
     //your class code here
}

Upvotes: 1

matt
matt

Reputation: 535989

var lines: [Line] = []

There's your undeclared type, all right. What is a Line? Swift has no idea (and neither do I, if it comes to that).

Upvotes: 0

Related Questions