【发布时间】:2017-02-14 12:26:27
【问题描述】:
我尝试在视图中画一条线,但由于可选类型错误,我的代码无法编译。我是 swift 和 Objective-c 的新手,我花了很多时间来搜索答案。问题到现在还没有解决。那么,谁能提供一些线索来解决这个问题?
代码:
import UIKit
class DrawLines: UIView {
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
//Drawing code
// context
let context = UIGraphicsGetCurrentContext()
CGContextSetLineWidth(context, 3.0)
CGContextSetStrokeColorWithColor(context, UIColor.purpleColor().cgColor)
//create a path
CGContextMoveToPoint(context,0,0)
CGContextAddLineToPoint(context,250,320)
}
}
错误:
【问题讨论】:
-
只需点击Fix-it选项即可。
-
@NiravD 感谢您的回答。它可能会通过您的建议解决问题,但它不能解决我的问题。你能告诉我导致这个问题的原因吗?
-
你的意思是它不能解决我的问题,修复后它会消除你当前遇到的错误。
-
@NiravD 错误消失了,但我的代码已更改。 CGContextSetLineWidth 更改为 context.setLineWidth。似乎从未使用过 CGContextSet。
-
检查@vadian 的答案。