简体   繁体   English

不安全指针<UInt8> Swift 3 中的初始化程序

[英]UnsafePointer<UInt8> initializer in Swift 3

I have a receipt validation class that is deprecated since Swift 3 has released.我有一个自 Swift 3 发布以来已弃用的收据验证类。 I fixed some issues, but I still have many ...我修复了一些问题,但我仍然有很多......

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这是我使用的 GitHub 源代码: https : //gist.github.com/baileysh9/4386ea92b047d97c7285#file-parsing_productids-swifthttps://gist.github.com/baileysh9/eddcba49d544635b3cf5

  1. First Error :第一个错误:

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

Compiler throws : Cannot invoke initializer for type UnsafePointer(UInt8) with an argument list of type UnsafeRawPointer编译器抛出:无法使用 UnsafeRawPointer 类型的参数列表调用 UnsafePointer(UInt8) 类型的初始化程序

  1. Second error第二个错误

    while (ptr < end)

Binary operators < cannot be applied to two UnsafePointer(UInt8) operands二元运算符 < 不能应用于两个 UnsafePointer(UInt8) 操作数

Thank you very much in advance :)非常感谢您提前:)

EDIT编辑

Thanks to LinShiwei answer I found a solution to UnsafePointer declaration.感谢 LinShiwei 的回答,我找到了 UnsafePointer 声明的解决方案。 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. In Swift 3, you cannot init an UnsafePointer using an UnsafeRawPointer .在斯威夫特3,你不能初始化一个UnsafePointer使用UnsafeRawPointer

    You can use assumingMemoryBound(to:) to convert an UnsafeRawPointer into an UnsafePointer<T> .您可以使用assumingMemoryBound(to:)UnsafeRawPointer转换为UnsafePointer<T> Like this:像这样:

     var ptr = data.bytes.assumingMemoryBound(to: UInt8.self)
  2. Use debugDescription or distance(to:) to compare two pointer.使用debugDescriptiondistance(to:)来比较两个指针。

     while(ptr.debugDescription < endPtr.debugDescription)

    or

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

It is possible to put a regular pointer sign i C & in front of a Int8 array or Uint8 array to make a pointer to supply a C-function input.可以在 Int8 数组或 Uint8 数组前面放置一个常规指针符号 i C &以创建一个提供 C 函数输入的指针。 Like the &aBuffer array below (but the data array must copied locally first, to keep control of the storage of the data storage until finished with the operation, you get an error else).像下面的 &aBuffer 数组(但数据数组必须先复制到本地,以保持对数据存储的存储控制,直到操作完成,否则会出错)。 Here in a routine handling dropInteraction data (delivering a byte array):在处理 dropInteraction 数据的例程中(传递字节数组):

func interpretInstanceData(filename: String, Buffer: [UInt8]) -> String {
    var aBuffer = Buffer
    let sInstanceData = String(cString: Ios_C_InterpretInstanceData(filename, &aBuffer, Int32(aBuffer.count)))

The question is slightly old.这个问题有点老了。 But googling for a solution on the topic of converting a swift byte array to a C-pointer (what else is UnsafePointer< UInt8 >).但是在谷歌上搜索关于将 swift 字节数组转换为 C 指针的解决方案(还有什么是 UnsafePointer< UInt8 >)。 This question made a hit.这个问题引起了轰动。 But I think this answer is helpful for later editions of Swift (that I use).但我认为这个答案对 Swift 的后续版本(我使用的)很有帮助。 Would have worked even then.即使到那时也会工作。 Worked in any kind of use needing a pointer from swift (just make the array of right type first).在需要来自 swift 的指针的任何类型的使用中工作(只需首先创建正确类型的数组)。

May have recently changed to just this, without the ".bytes."最近可能已更改为仅此,没有“.bytes”。 part:部分:

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

from the original:来自原文:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM