无法将'UInt'类型的值转换为预期的参数类型'UnsafeMutablePointer< UInt>' [英] Cannot convert value of type 'UInt' to expected argument type 'UnsafeMutablePointer<UInt>'

查看:1200
本文介绍了无法将'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)


这篇关于无法将'UInt'类型的值转换为预期的参数类型'UnsafeMutablePointer&lt; UInt&gt;'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆