无法将'UInt'类型的值转换为预期的参数类型'UnsafeMutablePointer< UInt>'
我正在尝试从URL可访问的内容初始化String:
I am trying to initialize a String from the content accessible by a URL:
actualresponse.response = String(contentsOfURL: url, usedEncoding: NSUTF8StringEncoding)
我收到以下错误,指向usedEncoding:
I get the following error thrown, pointing at the usedEncoding:
无法将'UInt'类型的值转换为预期的参数类型'UnsafeMutablePointer'
Cannot convert value of type 'UInt' to expected argument type 'UnsafeMutablePointer'
谁能告诉我为什么会抛出这个错误以及如何解决它?
Can anyone tell me why this error is thrown and how I can fix it?
有两种相似但不同的方法可以弄错。
There are two similar but different methods which can be mistaken.
-
通常的方法是
The usual method is
init(contentsOfURL url: NSURL,
encoding enc: UInt) throws
编码
参数采用 NSStringEncoding
值来指定编码,例如
The encoding
parameter takes an NSStringEncoding
value to specify the encoding, for example
let string = try? String(contentsOfURL:url, encoding:NSUTF8StringEncoding)
-
第二种方法通过将指针作为
usedEncoding传递来从文件中检索
编码参数
The second method retrieves the encoding from the file by passing a pointer as
usedEncoding
parameter
init(contentsOfURL url: NSURL,
usedEncoding enc: UnsafeMutablePointer<UInt>) throws
文档说:
返回后,如果成功读取url,则包含使用
来解释数据的编码。
Upon return, if url is read successfully, contains the encoding used to interpret the data.
这意味着您必须传递一个指针,该指针将包含确定的文件编码。
That means you have to pass a pointer which will contain the determined encoding of the file.
var encoding : NSStringEncoding = 0
let string = try? String(contentsOfURL:url, usedEncoding:&encoding)