Carpsen90 Carpsen90 - 7 months ago 72
Swift Question

Ambiguous use of 'init' with CFStringTransform and Swift 3

When trying to build the sample project of BonMot,

let theCFMutableString = NSMutableString(string: myString) as CFMutableString
CFStringTransform(theCFMutableString, UnsafeMutablePointer<CFRange>(nil), kCFStringTransformToUnicodeName, false)

I get this error on the

Ambiguous use of 'init'

The Xcode 8 project uses Swift 3


In Swift 2, pointer types conformed to NilLiteralConvertible, allowing a non-optional pointer type to represent a null pointer. Therefore when you did


the compiler was actually using the init(_ other: COpaquePointer) initialiser of UnsafeMutablePointer, as COpaquePointer is NilLiteralConvertible and can therefore represent a null pointer.

However in Swift 3 (SE-0055), pointer types no longer conform to ExpressibleByNilLiteral. Rather than allowing a non-optional pointer type to represent a null pointer, this is now simply done with optionals, where nil means a null pointer.

Therefore you can just pass nil directly into the range parameter of CFStringTransform, as it expects a UnsafeMutablePointer<CFRange>!:

CFStringTransform(theCFMutableString, nil, kCFStringTransformToUnicodeName, false)