Swift 3中的UnsafePointer <UInt8>初始化程序

Gra*_*Fox 22 unsafe-pointers ios swift3

我有一个收据验证类,自Swift 3发布以来已弃用.我解决了一些问题,但我还有很多......

这是我使用的GitHub源代码:https://gist.github.com/baileysh9/4386ea92b047d97c7285#file-parsing_productids-swifthttps://gist.github.com/baileysh9/eddcba49d544635b3cf5

  1. 第一个错误:

        var p = UnsafePointer<UInt8>(data.bytes)
    
    Run Code Online (Sandbox Code Playgroud)

编译器抛出:无法使用类型UnsafeRawPointer的参数列表调用类型UnsafePointer(UInt8)的初始化程序

  1. 第二个错误

    while (ptr < end)
    
    Run Code Online (Sandbox Code Playgroud)

二进制运算符<不能应用于两个UnsafePointer(UInt8)操作数

非常感谢你提前:)

编辑

感谢LinShiwei的回答,我找到了UnsafePointer声明的解决方案.它编译但尚未测试(因为其他错误避免我测试):

 func getProductIdFromReceipt(_ data:Data) -> String?
{
  let tempData: NSMutableData = NSMutableData(length: 26)!
  data.withUnsafeBytes {
        tempData.replaceBytes(in: NSMakeRange(0, data.count), withBytes: $0)
    }

    var p: UnsafePointer? = tempData.bytes.assumingMemoryBound(to: UInt8.self)
Run Code Online (Sandbox Code Playgroud)

Lin*_*wei 40

  1. 在Swift 3中,你无法初始化UnsafePointer使用UnsafeRawPointer.

    您可以使用assumingMemoryBound(to:)将其UnsafeRawPointer转换为UnsafePointer<T>.像这样:

    var ptr = data.bytes.assumingMemoryBound(to: UInt8.self)
    
    Run Code Online (Sandbox Code Playgroud)
  2. 使用debugDescriptiondistance(to:)比较两个指针.

    while(ptr.debugDescription < endPtr.debugDescription)
    
    Run Code Online (Sandbox Code Playgroud)

    要么

    while(ptr.distance(to:endPtr) > 0)
    
    Run Code Online (Sandbox Code Playgroud)