Swift 3 changes for getBytes method

♀尐吖头ヾ 提交于 2019-12-20 18:58:10

问题


I have tried to run the below code in 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)

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

What will be the solution ?


回答1:


For Swift3 just use following:

let array = [UInt8](yourDataObject)

That's all, folks!)




回答2:


It means that the arguments order has been reversed in Swift 3.

For 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)


来源:https://stackoverflow.com/questions/38097710/swift-3-changes-for-getbytes-method

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!