0
继 'unsafeBitCast' 撞车时使用CoreText方法时使用CoreText的CTRunDelegate
歇在最后一行'unsafeBitCast(...)',
控制台登录代码“致命错误:'不能类型之间unsafeBitCast不同尺寸'
let str = " 2"
let mutableAttrStr = NSMutableAttributedString(string: str)
var callBack = CTRunDelegateCallbacks(version: kCTRunDelegateVersion1, dealloc:
{ (obj) in
}, getAscent: { (obj) -> CGFloat in
return 40
}, getDescent: { (obj) -> CGFloat in
return 30
}) { (obj) -> CGFloat in
return 20
}
var imgUrl = "http://img3.3lian.com/2013/c2/64/d/65.jpg"
let callDelegate = CTRunDelegateCreate(&callBack,&imgUrl)
mutableAttrStr.addAttribute(kCTRunDelegateAttributeName as String, value: callDelegate!, range: NSRange.init(location: 0, length: 1))
...
...
...
let attributes = CTRunGetAttributes(run as! CTRun) as NSDictionary
let delegate = attributes.value(forKey: kCTRunDelegateAttributeName as String)
let imgPointer = CTRunDelegateGetRefCon(delegate as! CTRunDelegate)
let urlStr:String = unsafeBitCast(imgPointer, to: String.self);
imgPointer是实际UnsafeMutableRawPointer类型,但我必须把它转换成字符串告诉编译器
如果我这样做:
let imgPointer = CTRunDelegateGetRefCon(delegate as! CTRunDelegate) as! String
不幸的是,编译器警告:
任何帮助都很感激!
'imgPointer'不是'String',它是一个指向字符串的指针。你可以通过'let urlStr = imgPointer.load(as:String.self)来访问它指向的字符串' – dan
@dan你能告诉我'imgPointer.load(as:String.self)有什么区别吗?和'unsafeBitCast(imgPointer,to:String.self)'? –