Cannot convert value of type 'UInt' to expected argument type 'UnsafeMutablePointer'

前端 未结 1 480
闹比i
闹比i 2020-12-21 17:44

I am trying to initialize a String from the content accessible by a URL:

actualresponse.response = String(contentsOfURL: url, usedEncoding: NSUTF8StringEncod         


        
相关标签:
1条回答
  • 2020-12-21 18:05

    There are two similar but different methods which can be mistaken.

    • The usual method is

      init(contentsOfURL url: NSURL,
          encoding enc: UInt) throws
      

      The encoding parameter takes an NSStringEncoding value to specify the encoding, for example

      let string = try? String(contentsOfURL:url, encoding:NSUTF8StringEncoding)
      

    • 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
      

      The documentation says:

      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)
      
    0 讨论(0)
提交回复
热议问题