MemoryLayout<Type>is a generic type evaluated at compile time that determines the
strideof each specified
Unsafe Swift pointers use a very predictable naming scheme so that you know what the traits of the pointer are. Mutable or immutable, raw or typed, buffer style or not. In total there is a combination of eight of these.
This example is similar to the previous one, except that it first creates a raw pointer. The typed pointer is created by binding the memory to the required type
Int. By binding memory, it can be accessed in a type-safe way. Memory binding is done behind the scenes when you create a typed pointer.
Never bind memory to two unrelated types at once. This is called Type Punning and Swift does not like puns. Instead, you can temporarily rebind memory with a method like
withMemoryRebound(to:capacity:). Also, the rules say it is illegal to rebind from a trivial type (such as an
Int) to a non-trivial type (such as a
class). Don’t do it.
Stay up-to-date by subscribing to the Comments RSS Feed for this post.