4

I am getting a build error, when I define the line

 let runFont : CTFontRef = CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttributeName)

And the error is: Cannot convert value of type 'CFString' to expected argument type 'UnsafePointer' (aka 'UnsafePointer<()>')

Luca.A
  • 59
  • 1
  • 6
Aryan Kashyap
  • 157
  • 3
  • 11
  • An `UnsafePointer` error usually means that the affected parameter is used as an inout pointer. Read the documentation by QuickHelp, ⌥-click or ⌘-click on the symbol or pressing ⇧⌘0 and typing the method name. – vadian Mar 30 '16 at 13:39
  • Hi @vadian can you please give me a code example of how to solve it.. i have looked at the documentation and tries many ways but doesnt work :( – Aryan Kashyap Mar 30 '16 at 16:55

2 Answers2

7

Try

let runFont = unsafeBitCast(CFDictionaryGetValue(CTRunGetAttributes(run), unsafeBitCast(kCTFontAttributeName, UnsafePointer<Void>.self)), CTFontRef.self)
vadian
  • 274,689
  • 30
  • 353
  • 361
4

Swift 3 version of @vadian answer:

let runFont = unsafeBitCast(CFDictionaryGetValue(CTRunGetAttributes(run), unsafeBitCast(kCTFontAttributeName, to: UnsafeRawPointer.self)), to: CTFont.self)
feca
  • 1,119
  • 16
  • 14