1

I have to use CFDictionaryCreate method in Swift(documentation link).

I am having a hard time to initialize the input parameters correctly in order to pass parameters(keys and values) of type UnsafeMutablePointer<UnsafePointer<Void>>.

Here is my code:

 var font_name: CFStringRef! = CFStringCreateWithCString(nil, "Courier", kCFStringEncodingASCII) var font: CTFontRef! = CTFontCreateWithName(font_name, 25.0, nil) var keys: [UnsafePointer<Void>] = ???? // how to intialize with "kCTFontAttributeName" var values: [UnsafePointer<Void>] = ???? // how to intialize with "font" variable var keyCallBacks = kCFTypeDictionaryKeyCallBacks var valueCallBacks = kCFTypeDictionaryValueCallBacks var font_attributes: CFDictionaryRef! = CFDictionaryCreate(kCFAllocatorDefault, &keys, &values, sizeofValue(keys), &keyCallBacks, &valueCallBacks) var attr_string: CFAttributedStringRef! = CFAttributedStringCreate(nil, "hello", font_attributes) 
4
  • Are you sure that you cannot use a Swift dictionary instead? A Swift [NSString : AnyObject] dictionary is automatically bridged to NSDictionary (and vice versa), and NSDictionary is toll-free bridged with CFDictionary. – Perhaps you should show your concrete case. Commented May 9, 2015 at 18:26
  • @MartinR, I have added the code. I am not sure if I can use NSDictionary in the above code. Can you help me out here? Commented May 9, 2015 at 18:52
  • What do you need the font_attributes dictionary for? Commented May 9, 2015 at 18:55
  • I need font_attributes to create following ........ var attr_string: CFAttributedStringRef! = CFAttributedStringCreate(nil, "hello", font_attributes)............Added code in question too Commented May 9, 2015 at 19:00

1 Answer 1

5

You can simply use a Swift dictionary of type [ NSString : AnyObject ], which is automatically bridged to NSDictionary or CFDictionary. Note that you don't need CFStringRef either.

let font = CTFontCreateWithName("Courier", 25.0, nil) let attributes : [ NSString : AnyObject ] = [ kCTFontAttributeName : font ] let attrString = CFAttributedStringCreate(nil, "Hello", attributes) 

Alternatively,

let attrString = NSAttributedString(string: "Hello", attributes: attributes) 

because NSAttributedString is toll-free bridged with CFAttributedString.


Just for the sake of completeness, here is how you could use CFDictionaryCreate():

let font = CTFontCreateWithName("Courier", 25.0, nil) var keys = [ unsafeAddressOf(kCTFontAttributeName) ] var values = [ unsafeAddressOf(font) ] var keyCallbacks = kCFTypeDictionaryKeyCallBacks var valueCallbacks = kCFTypeDictionaryValueCallBacks let attributes = CFDictionaryCreate(nil, &keys, &values, 1, &keyCallbacks, &valueCallbacks) let attrString = CFAttributedStringCreate(nil, "Hello", attributes) 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Genius! It worked great. Just to increase my knowledge, any idea how to deal with parameters of type UnsafeMutablePointer<UnsafePointer<Void>>. Thanks again :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.