24653

How to pass parameter of type UnsafeMutablePointer

Question:

I have to use CFDictionaryCreate method in Swift(<a href="https://developer.apple.com/library/mac/documentation/CoreFoundation/Reference/CFDictionaryRef/index.html#//apple_ref/c/func/CFDictionaryCreate" rel="nofollow">documentation link</a>).

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)

Answer1:

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.

<hr />

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)

Recommend

  • iPhone DNS resolution, not all A records returned
  • Advice on Debugging a UIAppFonts Issue on iPad
  • Identify extension of a file without extension
  • Pyglet not running properly on AMD HD4250
  • how to edit a phone number values programmatically from address book ios
  • Why do I get the wrong color of a pixel with following code?
  • How to programmatically retrieve UUID of root disk partition in OS X?
  • Problems with OpenCV FileStorage in XCode 4
  • How to project IR image on a 2D plane using OpenCV and PCL
  • Unfinalized objects exhausting memory
  • Row Count Is Returning the incorrect number using RaptureXML
  • Obtain ObjectIdHex value from mgo query
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Join two tables and save into third-sql
  • JSON with duplicate key names losing information when parsed
  • Display Images one by one with next and previous functionality
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Matplotlib draw Spline from multiple points
  • XCode can't find symbols for a specific iOS library/framework project
  • Calling of Constructors in a Java
  • Compare two NSDates in iPhone
  • Transpose CSV data with awk (pivot transformation)
  • Use group_by to filter specific cases while keeping NAs
  • How to delete a row from a dynamic generate table using jquery?
  • log4net write single file for each call to log.info
  • Benchmarking RAM performance - UWP and C#
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Acquiring multiple attributes from .xml file in c#
  • using HTMLImports.whenReady not working in chrome
  • How do I configure my settings file to work with unit tests?
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Authorize attributes not working in MVC 4
  • How can I remove ASP.NET Designer.cs files?
  • Bitwise OR returns boolean when one of operands is nil
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • failed to connect to specific WiFi in android programmatically
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?