【问题标题】:How to use Implicitly Unwrapped Optionals?如何使用隐式解包选项?
【发布时间】:2014-07-23 21:03:35
【问题描述】:

我正在将 NSView 移植到 Swift,我需要使用 Quartz CGContextAddPath。

import Cocoa

class MYView: NSView {

    init(frame: NSRect) {
        super.init(frame: frame)
    }

    override func drawRect(dirtyRect: NSRect) {
        super.drawRect(dirtyRect)


        NSColor .redColor() .set()
        NSRectFill(self.bounds)
        var p :CGMutablePathRef = CGPathCreateMutable()
        var ctx =  NSGraphicsContext.currentContext().graphicsPort()
        CGContextAddPath(ctx, p) // compiler rejects this line

    }

}

你如何理解这个错误信息?

Cannot convert the expression's type 'Void' to type 'CGContext!'

CGContextAddPath 的 Swift 签名是:

func CGContextAddPath(context: CGContext!, path: CGPath!)

我的错误是什么?

当我使用这个时:

let context = UnsafePointer<CGContext>(ctx).memory

我现在遇到运行时错误:

Jun 3 15:57:13 xxx.x SwiftTest[57092] &lt;Error&gt;: CGContextAddPath: invalid context 0x7fff73bd0060. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.

这是我目前正在使用的代码:

import Cocoa

class MYView: NSView {

    init(frame: NSRect) {
        super.init(frame: frame)
    }

    override func drawRect(dirtyRect: NSRect) {
        super.drawRect(dirtyRect)

        var p :CGMutablePathRef =  CGPathCreateMutableCopy( CGPathCreateWithRoundedRect(self.bounds, 10, 10, nil))
        var ctx =  NSGraphicsContext.currentContext().graphicsPort()
        let context = UnsafePointer<CGContext>(ctx).memory

        CGContextAddPath(context, p) // compiler no longer rejects this line
        var blueColor = NSColor.blueColor()
        CGContextSetStrokeColorWithColor(context, NSColor.blueColor().CGColor)
        CGContextSetLineWidth(context, 2)
        CGContextStrokePath(context)

    }

}

【问题讨论】:

    标签: swift


    【解决方案1】:

    使用NSGraphicsContext.currentContext().graphicsPort()。它返回 void*。你必须把它投射到 CGContextRef

    let ctx = UnsafePointer<CGContext>(NSGraphicsContext.currentContext().‌​graphicsPort()).memo‌​ry
    

    【讨论】:

    • Swift 是一种相对较新的语言。您能否准确解释一下如何将其转换为 CGContextRef ?
    • 这是个好问题。试试这个:让 ctx = UnsafePointer(NSGraphicsContext.currentContext().graphicsPort()).memory
    • 这不起作用(不绘制),并导致此错误:这是一个严重的错误。此应用程序或它使用的库正在使用无效的上下文,从而导致系统稳定性和可靠性的整体下降。此通知是出于礼貌:请解决此问题。这将成为即将到来的更新中的致命错误。
    【解决方案2】:

    从 Swift 1.0 开始

    如果您的部署目标是 10.10,您可以使用 Yosemite 引入的便捷方法。

    let context = NSGraphicsContext.currentContext().CGContext
    

    如果您必须支持 10.9,则必须按照以下说明手动转换上下文。

    let contextPtr = NSGraphicsContext.currentContext().graphicsPort
    let context = unsafeBitCast(contextPtr, CGContext.self)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多