I'm trying to have a helper method for Data class to convert bytes to UInt32. So far this worked:
extension Data {
var uint32:UInt32 {
return UInt32(littleEndian: self.withUnsafeBytes { (ptr: UnsafePointer<UInt32>) in ptr.pointee })
}
}
Since swift 5 the compiler gives the following warning:
'withUnsafeBytes' is deprecated: use `withUnsafeBytes<R>(_: (UnsafeRawBufferPointer) throws -> R) rethrows -> R` instead`
After some research I tried to use withUnsafeMutableBytes along with UnsafeMutablePointer<UInt32>
but this gives an error: Cannot use mutating member on immutable value: 'self' is immutable. (This would work if it wasn't a part of an inmutable getter function)
How can I fix my read only helper variable so the compiler doesn't give warnings?
(Of course the same applies to UInt8 or UInt16)