user2632918
user2632918

Reputation:

Variable is not convertible to CGContext in Swift

I will draw lines on a view in a iOS-Application.

xcode write, that line is not convertible to CGContext.

Should I convert line? When I have to do, then i hope you have a tip for me.

This is my DrawView.swift:

class DrawView: UIView {

var lines: [Line] = []
var lastPoint: CGPoint!

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    let t = touches.anyObject() as UITouch
    lastPoint = t.locationInView(self)
}

override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
    let t = touches.anyObject() as UITouch
    var newPoint = t.locationInView(self)
    lines.append(Line(start: lastPoint, end: newPoint))

    lastPoint = newPoint

    self.setNeedsDisplay()
}

override func drawRect(rect: CGRect) {
    var context = UIGraphicsGetCurrentContext()

    CGContextBeginPath(context)

    for line in lines {
        //Here is the error       'line' is not convertible to CGContext
        CGContextMoveToPoint(line, line.start.x, line.start.y)
        CGContextAddLineToPoint(line, line.end.x, line.end.y)
    }

    CGContextSetRGBStrokeColor(context, 1, 1, 0, 1)
    CGContextSetLineWidth(context, 5)
    CGContextStrokePath(context)

}
}

Here is the Line.swift

import UIKit

class Line {

var start: CGPoint
var end: CGPoint

init(start _start: CGPoint, end _end: CGPoint) {
    start = _start
    end = _end
}


}

and this is the ViewController.swift

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


@IBAction func clearTapped () {
    NSLog("This button is tapped")
}

}

Upvotes: 0

Views: 452

Answers (1)

Vladimir
Vladimir

Reputation: 170839

First parameter to CGContextXXX functions is CGContext you operate on. In your case you probably want to use context variable created few lines above:

CGContextMoveToPoint(context, line.start.x, line.start.y)

Upvotes: 0

Related Questions