I would like to create a function like this:
func getStride(_ type: T.Type) {
return MemoryLayout<type>.stride
}
But it doesn't work .
I would like to be able to call it like this:
getStride(Bool) //=> stride value
If not that, then perhaps this:
getStride(Bool.self)
Wondering if anything like this is possible.
You have to define a generic function, then you can refer to MemoryLayout<T>
with the same type that was passed to the function:
func getStride<T>(_ type: T.Type) -> Int {
return MemoryLayout<T>.stride
}
print(getStride(Bool.self)) // 1
print(getStride(Double.self)) // 8
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.