I'm hoping someone can help me figure out why I'm having such a hard time passing a variable to my NSView. Essentially, I am trying to set a variable ( myColor
) to set the color of a circle I am drawing. The code appears like so;
import Cocoa
class Circle: NSView {
var myColor:NSColor? = NSColor.greenColor()
lazy var innerRing: CAShapeLayer = {
let innerRing = CAShapeLayer()
let circleRadius: CGFloat = 20.0
innerRing.frame = self.bounds
var circleFrame = CGRect(x: 0, y: 0, width: circleRadius, height: circleRadius)
circleFrame.origin.x = CGRectGetMidX(self.bounds) - CGRectGetMidX(circleFrame)
circleFrame.origin.y = CGRectGetMidY(self.bounds) - CGRectGetMidY(circleFrame)
innerRing.path = CGPathCreateWithEllipseInRect(circleFrame, nil)
innerRing.lineWidth = 3.0
innerRing.strokeStart = 0.0
innerRing.strokeEnd = 0.75
innerRing.fillColor = NSColor.clearColor().CGColor
innerRing.strokeColor = myColor.CGColor
//THIS LINE THROWS THE ERROR
return innerRing
}()
override func awakeFromNib() {
super.awakeFromNib()
wantsLayer = true
layer = CALayer()
layer?.addSublayer(innerRing)
}
}
I keep receiving the error Instance member 'myColor' cannot be used on type 'Circle' . If I set my strokeColor
to be something simple, like NSColor.blueColor().CGColor, it compiles without issue. But I've never had such issue setting a variable before, and I'm not sure what I'm doing wrong.
EDIT: Updating with solution
import Cocoa
class Circle: NSView {
lazy var innerRing: CAShapeLayer = {
let innerRing = CAShapeLayer()
let circleRadius: CGFloat = 20.0
innerRing.frame = self.bounds
var circleFrame = CGRect(x: 0, y: 0, width: circleRadius, height: circleRadius)
circleFrame.origin.x = CGRectGetMidX(self.bounds) - CGRectGetMidX(circleFrame)
circleFrame.origin.y = CGRectGetMidY(self.bounds) - CGRectGetMidY(circleFrame)
innerRing.path = CGPathCreateWithEllipseInRect(circleFrame, nil)
innerRing.lineWidth = 2.0
innerRing.strokeStart = 0.0
innerRing.strokeEnd = 0.75
innerRing.fillColor = NSColor.greenColor().CGColor
innerRing.strokeColor = NSColor.blackColor().CGColor
return innerRing
}()
override func awakeFromNib() {
super.awakeFromNib()
wantsLayer = true
layer = CALayer()
layer?.addSublayer(innerRing)
}
}
I am then calling this from another View Controller my using cirleView.innerRing.strokeColor = NSColor.redColor().CGColor
, eliminating the need for a separate myColor
variable altogether.
Your myColor
is typed as an Optional ( NSColor?
). You therefore have to unwrap it before you can get its CGColor
property. Also, you're in an anonymous function here, so you must refer explicitly to self
in order to access your property. Thus, this will fix the problem:
innerRing.strokeColor = self.myColor!.CGColor
Or, even better, don't type myColor
as an Optional; just make it an NSColor. You can do this because you are giving it a default value right there in the declaration. You then won't have anything to unwrap and the exclamation mark won't be needed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.