如何让 kCGPathStroke 在 Swift 2 中工作?

How to make kCGPathStroke work in Swift 2?

我在尝试将我的应用程序升级到 Swift 2 时收到两个错误。

两个错误是:

Use of unresolved identifier 'kCGPathStroke'

和:

Cannot convert value of type 'NSMutableDictionary' to expected argument type '[String : AnyObject]?'

这两个问题都在下面的代码中进行了评论。我检查了文档,看起来 kCGPathStroke 仍然存在,所以我真的很困惑为什么它被破坏了。我做错了什么?

import UIKit
import CoreGraphics
class MIBadgeLabel: UILabel {
    override func drawRect(rect: CGRect)
    {
        // Drawing code
        let ctx: CGContextRef = UIGraphicsGetCurrentContext()!
        let borderPath: UIBezierPath = UIBezierPath(roundedRect: rect, byRoundingCorners:UIRectCorner.AllCorners, cornerRadii: CGSizeMake(10.0, 10.0))

        CGContextSetFillColorWithColor(ctx, UIColor.whiteColor().CGColor)
        CGContextSaveGState(ctx)
        CGContextAddPath(ctx, borderPath.CGPath)
        CGContextSetLineWidth(ctx, 4.0)
        CGContextSetStrokeColorWithColor(ctx, UIColor.clearColor().CGColor)
        CGContextDrawPath(ctx, kCGPathStroke) //Swift 2 error
        CGContextRestoreGState(ctx)

        CGContextSaveGState(ctx)
//      CGContextSetFillColorWithColor(ctx, UIColor.whiteColor().CGColor)
        var textFrame: CGRect  = rect
        let labelString: NSString = self.text! as NSString
        let textSize: CGSize  = labelString.sizeWithAttributes([NSFontAttributeName : UIFont.systemFontOfSize(13.0)])
        textFrame.size.height = textSize.height

        textFrame.origin.y = rect.origin.y + ceil((rect.size.height - textFrame.size.height) / 2.0)

        let paragraphStyle: NSMutableParagraphStyle = NSMutableParagraphStyle();
        paragraphStyle.alignment = .Center

        var attributes: NSMutableDictionary = [NSFontAttributeName: UIFont.systemFontOfSize(13.0), NSForegroundColorAttributeName : UIColor.whiteColor(), NSParagraphStyleAttributeName:paragraphStyle]

        labelString.drawInRect(textFrame, withAttributes: attributes)  // Swift 2 error
    }
}

第一个错误:

CGContextDrawPath(ctx, kCGPathStroke)

像这样:

CGContextDrawPath(ctx, .Stroke)

第二个错误:

var attributes: NSMutableDictionary = [
    NSFontAttributeName: UIFont.systemFontOfSize(13.0), 
    NSForegroundColorAttributeName : UIColor.whiteColor(), 
    NSParagraphStyleAttributeName:paragraphStyle
]

像这样:

let attributes = [
    NSFontAttributeName: UIFont.systemFontOfSize(13.0), 
    NSForegroundColorAttributeName : UIColor.whiteColor(), 
    NSParagraphStyleAttributeName:paragraphStyle
]

要解决标识符 'kCGPathStroke' 问题,使用 Swift 2.0,使用:

CGContextDrawPath(ctx, CGPathDrawingMode.Stroke)