extension Data {
//generic method
func value<T>(from byteOffset: Int, as: T) -> T {
self.withUnsafeBytes { $0.load(fromByteOffset: byteOffset, as: T.self) }
}
//none-generic method
func uint16Value(from byteOffset: Int) -> UInt16 {
self.withUnsafeBytes { $0.load(fromByteOffset: byteOffset, as: UInt16.self) }
}
}
When I output the following:
let a = data.withUnsafeBytes { $0.load(fromByteOffset: 0, as: UInt16.self) }
let b = data.uint16Value(from: 0)
let c = data.value(from: 0, as: UInt16.self)
print(a, b, c)
The output will be 2022 2022 UInt16
.
When I am inspecting c
it turns out to be UInt16.Type
which does not make sense to me. Why would it return the Type instead of the value?