简体   繁体   English

Swift无法将类型“ CGPath”的值分配给类型“ CGPath?”

[英]Swift Cannot assign a value of type 'CGPath' to a value of type 'CGPath?'

I'm attempting to round just the top left and bottom left corners of a UITextField . 我试图只将UITextField左上角和左下角四舍五入。 Here is the code I am using: 这是我正在使用的代码:

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;
}

The line 线

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

is producing the error 正在产生错误

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

This is confusing me since I've been able to set values to optionals no problem elsewhere in the code. 这使我感到困惑,因为我已经能够将值设置为可选值,而在代码的其他地方也没问题。 Could someone help me understand what I am doing wrong? 有人可以帮助我了解我在做什么错吗?

如果您将Swift 2.0与Xcode Beta一起使用,则可能是编译器的一个错误,试图告诉您应该使用数组来指示拐角

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM