【发布时间】: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