I am doing some Swift 5 conversion on code that I don't quite understand, legacy code from a previous developer. I get:
'withUnsafeBytes' is deprecated: use withUnsafeBytes(_: (UnsafeRawBufferPointer) throws -> R) rethrows -> R` instead
for:
func toArray<T>(type: T.Type) -> [T] {
return self.withUnsafeBytes {
[T](UnsafeBufferPointer(start: $0, count: self.count/MemoryLayout<T>.stride))
}
}
I want to replace it with this but I am unsure if it does the same thing:
func toArray<T>(type: T.Type) -> [T] where T: ExpressibleByIntegerLiteral {
var array = [T](repeating: 0, count: self.count/MemoryLayout<T>.stride)
_ = array.withUnsafeMutableBytes { copyBytes(to: $0) }
return array
}
Used in the context of these two fxs:
static func extractPacketSizeFromIV(iv: Data) -> Int32? {
let array = iv.toArray(type: Int32.self)
guard array.count == 4 else { return nil }
let r0 = array[0].bigEndian
let r1 = array[1].bigEndian
let r2 = array[2].bigEndian
return r2 ^ r1 ^ r0
}
static func extractGuidFromIV(iv: Data) -> Int32? {
let array = iv.toArray(type: Int32.self)
guard array.count == 4 else { return nil }
let r0 = array[0].bigEndian
let r1 = array[1].bigEndian
let r2 = array[2].bigEndian
let r3 = array[3].bigEndian
return r3 ^ r2 ^ r1 ^ r0
}