使用核心文本的EXC_BAD_ACCESS错误

时间:2018-01-16 17:38:34

标签: ios swift mapkit core-graphics core-text

我在这段代码中得到了一个EXC_BAD_ACCESS错误,其中常量" line"被宣布。在表示"行的行:CTLine = CTLineCreateWithAttributedString(attrString)"。确切的错误是"线程12:EXC_BAD_ACCESS(代码= 1,地址= 0x7e8)"。

我可以帮助找出问题吗?

我也在类似问题的答案中看到了一些名为" Zombie"帮助调试。什么是僵尸,我该怎么做呢?

import UIKit
import MapKit
import CoreText

class LabelOverlayRenderer: MKOverlayRenderer {

    override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in context: CGContext) {

//        let labelOverlay = overlay as! LabelOverlay

        let string: CFString
        let font: CTFont

        let nsString = NSString(string: "Hello" as CFString)
        string = nsString as CFString

        font = CTFontCreateUIFontForLanguage(.label, 17, "US" as CFString)!

        var keys: [CFString] = [kCTFontAttributeName]
        var values: [CFTypeRef] = [font]

        var mutablePointerKeys: UnsafeMutablePointer<UnsafeRawPointer?>!
        var rawPointerKeys: UnsafeRawPointer?

        rawPointerKeys = UnsafeRawPointer?(&keys)
        mutablePointerKeys = UnsafeMutablePointer<UnsafeRawPointer?>!(&rawPointerKeys)

        var mutablePointerValues: UnsafeMutablePointer<UnsafeRawPointer?>!
        var rawPointerValues: UnsafeRawPointer?

        rawPointerValues = UnsafeRawPointer?(&values)
        mutablePointerValues = UnsafeMutablePointer<UnsafeRawPointer?>!(&rawPointerValues)

        var cfTypeDictionaryKeyCallBacks = CFDictionaryKeyCallBacks()
        var cfTypeDictionaryValueCallBacks = CFDictionaryValueCallBacks()

        let attributes: CFDictionary = CFDictionaryCreate(kCFAllocatorDefault, mutablePointerKeys, mutablePointerValues, MemoryLayout.size(ofValue: keys)/MemoryLayout.size(ofValue: keys[0]), &cfTypeDictionaryKeyCallBacks, &cfTypeDictionaryValueCallBacks)

        let attrString: CFAttributedString = CFAttributedStringCreate(kCFAllocatorDefault, string, attributes)

        let line: CTLine = CTLineCreateWithAttributedString(attrString) // * Error occurs here. It says "Thread 12: EXC_BAD_ACCESS (code=1, address=0x7e8)". 

        context.textPosition = CGPoint(x: 10, y: 10)
        CTLineDraw(line, context)

    }
}

1 个答案:

答案 0 :(得分:0)

当您想要访问当前已分配的变量时,您将拥有EXC_BAD_ACCESS,启用Zombie后您可以看到该变量,请参阅此内容以启用它nszombie