Swift 3中的UnsafePointer 初始化程序

我有一个自Swift 3发布以来不推荐使用的收据validation类。 我解决了一些问题,但我还有很多……

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

  1. 第一个错误:

    var p = UnsafePointer(data.bytes) 

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

  1. 第二个错误

     while (ptr < end) 

二进制运算符<不能应用于两个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) 

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

    您可以使用UnsafeRawPointer assumingMemoryBound(to:)UnsafeRawPointer转换为UnsafePointer 。 喜欢这个:

     var ptr = data.bytes.assumingMemoryBound(to: UInt8.self) 
  2. 使用debugDescriptiondistance(to:)来比较两个指针。

     while(ptr.debugDescription < endPtr.debugDescription) 

    要么

     while(ptr.distance(to:endPtr) > 0) 

最近可能改为这样,没有“.bytes”。 部分:

 var p: UnsafePointer = data.assumingMemoryBound(to: UInt8.self) 

来自原文:

 var p = UnsafePointer(data.bytes)