ios Swift 3 对 getBytes 方法的更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38097710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift 3 changes for getBytes method
提问by iDev
I have tried to run the below code in swift 3
我试图在 swift 3 中运行以下代码
var values = [UInt8](count:data!.length, repeatedValue:0)
data!.getBytes(&values, length:data!.length)
where data is 'Data' datatype (NSData is change to 'Data' as per swift 3 guidelines)
其中 data 是 'Data' 数据类型(NSData 根据 swift 3 指南更改为 'Data')
I am not able to run the above code in Swift 3. Compiler gives error that "Argument Repeated value must precede argument". The same line of code was working in Swift 2.2
我无法在 Swift 3 中运行上述代码。编译器给出“参数重复值必须先于参数”的错误。同一行代码在 Swift 2.2 中工作
What will be the solution ?
解决方案是什么?
回答by sVd
For Swift3 just use following:
对于 Swift3,只需使用以下内容:
let array = [UInt8](yourDataObject)
That's all, folks!)
就是这样,伙计们!)
回答by Eric Aya
It means that the arguments order has been reversed in Swift 3.
这意味着参数顺序在 Swift 3 中被颠倒了。
For NSData:
对于 NSData:
var values = [UInt8](repeating:0, count:data!.length)
data.getBytes(&values, length: data!.length)
For Data:
对于数据:
var values = [UInt8](repeating:0, count:data!.count)
data.copyBytes(to: &values, count: data!.count)