Swift 3中的UnsafePointer 初始化程序
我有一个自Swift 3发布以来不推荐使用的收据validation类。 我解决了一些问题,但我还有很多……
这是我使用的GitHub源代码: https : //gist.github.com/baileysh9/4386ea92b047d97c7285#file-parsing_productids-swift和https://gist.github.com/baileysh9/eddcba49d544635b3cf5
-
第一个错误:
var p = UnsafePointer(data.bytes)
编译器抛出:无法使用类型UnsafeRawPointer的参数列表调用类型UnsafePointer(UInt8)的初始化程序
-
第二个错误
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)
-
在Swift 3中,您无法使用
UnsafeRawPointer
初始化UnsafeRawPointer
。您可以使用
UnsafeRawPointer
assumingMemoryBound(to:)
将UnsafeRawPointer
转换为UnsafePointer
。 喜欢这个:var ptr = data.bytes.assumingMemoryBound(to: UInt8.self)
-
使用
debugDescription
或distance(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)
- Mach-O符号存根(IOS)
- 在Objective C的自动布局中使用约束来支持RTL语言
- 为什么iOS应用程序包中的PNG图像比在我的项目中大?
- 在Swift中的PageViewController当前页面索引
- Firebase电子邮件validation不会validation帐户
- 基于Swift NSLayoutConstraint方程的常数
- 使用Core Graphics绘制部分图像或对angular线剪切
- UIApplicationBackgroundRefreshStatusDidChangeNotification用法没有相应的委托方法
- 在GoustoTech与团队见面-Spencer MacDonald-移动主管