UnsafePointer< UINT8> Swift 3中的初始化程序 [英] UnsafePointer<UInt8> initializer in Swift 3

查看:130
本文介绍了UnsafePointer< UINT8> Swift 3中的初始化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个收据验证类,自Swift 3发布以来已弃用。我修复了一些问题,但我仍然有很多...

I have a receipt validation class that is deprecated since Swift 3 has released. I fixed some issues, but I still have many ...

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

Here is the GitHub source code I used : https://gist.github.com/baileysh9/4386ea92b047d97c7285#file-parsing_productids-swift and https://gist.github.com/baileysh9/eddcba49d544635b3cf5


  1. 第一个错误:

  1. First Error :

    var p = UnsafePointer<UInt8>(data.bytes)


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

Compiler throws : Cannot invoke initializer for type UnsafePointer(UInt8) with an argument list of type UnsafeRawPointer


  1. 第二个错误

  1. Second error

while (ptr < end)


二元运算符<无法提前应用于两个UnsafePointer(UInt8)操作数

Binary operators < cannot be applied to two UnsafePointer(UInt8) operands

非常感谢

编辑

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

Thanks to LinShiwei answer I found a solution to UnsafePointer declaration. It compiles but not tested yet (because other errors avoid me to test) :

 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 初始化 UnsafePointer

您可以使用 assumeMemoryBound(to:)转换 UnsafeRawPointer 进入 UnsafePointer< T> 。像这样:

You can use assumingMemoryBound(to:) to convert an UnsafeRawPointer into an UnsafePointer<T>. Like this:

var ptr = data.bytes.assumingMemoryBound(to: UInt8.self)


  • 使用 debugDescription 距离(至: )比较两个指针。

    while(ptr.debugDescription < endPtr.debugDescription)
    

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


  • 这篇关于UnsafePointer&LT; UINT8&GT; Swift 3中的初始化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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