【问题标题】:EXC_BAD_ACCESS error using core text使用核心文本的 EXC_BAD_ACCESS 错误
【发布时间】:2018-01-16 17:38:33
【问题描述】:

在声明常量“行”的末尾处,这段代码出现了 EXC_BAD_ACCESS 错误。在写着“让行:CTLine = CTLineCreateWithAttributedString(attrString)”的那一行。确切的错误是“线程 12:EXC_BAD_ACCESS (code=1, address=0x7e8)”。

我可以得到一些帮助来确定问题吗?

我还在类似问题的答案中看到了一些名为“Zombie”的东西来帮助调试。什么是 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)

    }
}

【问题讨论】:

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


    【解决方案1】:

    当你想访问一个当前被取消分配的变量时,你将拥有 EXC_BAD_ACCESS ,启用 Zombie 后你可以看到该变量,查看此以启用它nszombie

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 1970-01-01
      • 2012-07-25
      • 2019-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-11
      • 1970-01-01
      相关资源
      最近更新 更多