本文介绍了'init' 与 CFStringTransform 和 Swift 3 的模糊使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试构建BonMot的示例项目时,

When trying to build the sample project of BonMot,

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

我在 CFStringTransform 行上收到此错误

I get this error on the CFStringTransform line

Ambiguous use of 'init'

Xcode 8 项目使用 Swift 3

The Xcode 8 project uses Swift 3

推荐答案

在 Swift 2 中,指针类型符合 NilLiteralConvertible,允许非可选指针类型表示空指针.因此,当你这样做

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

UnsafeMutablePointer<CFRange>(nil)

编译器实际上使用了 UnsafeMutablePointerinit(_ other: COpaquePointer) 初始化器,因为 COpaquePointerNilLiteralConvertible,因此可以表示空指针.

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

但是在 Swift 3 (SE-0055),指针类型不再符合 ExpressibleByNilLiteral.现在不再允许非可选指针类型表示空指针,而是使用可选类型来完成,其中 nil 表示空指针.

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.

因此,您可以将 nil 直接传递到 CFStringTransformrange 参数中,因为它需要一个 UnsafeMutablePointer!:

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

CFStringTransform(theCFMutableString, nil, kCFStringTransformToUnicodeName, false)

这篇关于'init' 与 CFStringTransform 和 Swift 3 的模糊使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 14:44