Swift无法指定类型' CGPath'的值。值为' CGPath?'

时间:2015-08-26 15:22:06

标签: ios swift uitextfield uibezierpath cashapelayer

我试图绕过UITextField的左上角和左下角。这是我正在使用的代码:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    let maskLayer = CAShapeLayer()
    maskLayer.path = UIBezierPath(roundedRect: locationText.bounds, byRoundingCorners: .TopLeft | .BottomLeft, cornerRadii: CGSize(width: 10.0, height: 10.0)).CGPath

    locationText.layer.mask = maskLayer;
}

该行

maskLayer.path = UIBezierPath(roundedRect: locationText.bounds, byRoundingCorners: .TopLeft | .BottomLeft, cornerRadii: CGSize(width: 10.0, height: 10.0)).CGPath

正在产生错误

Cannot assign a value of type 'CGPath' to a value of type 'CGPath?'

这让我感到困惑,因为我已经能够将值设置为选项,而代码中的其他地方没有问题。有人能帮助我理解我做错了吗?

1 个答案:

答案 0 :(得分:3)

如果您在Xcode beta版中使用swift 2.0,那么编译器可能会错误地告诉您应该使用数组来指示角落

maskLayer.path = UIBezierPath(roundedRect: self.view.bounds, byRoundingCorners: [.TopLeft, .BottomLeft], cornerRadii: CGSize(width: 10.0, height: 10.0)).CGPath