In order to integrate with C API\'s while using Swift, I need to use the sizeof function. In C, this was easy. In Swift, I am in a labyrinth of type errors.
I have
Updated for Swift 3
Be careful that MemoryLayout means something different than sizeof in C/Obj-C. You can read this old thread https://devforums.apple.com/message/1086617#1086617
Swift uses an generic type to make it explicit that the number is known at compile time.
To summarize, MemoryLayout is the space required for a single instance while MemoryLayout is the distance between successive elements in a contiguous array. MemoryLayout in Swift is the same as sizeof(type) in C/Obj-C.
To give a more concrete example:
struct Foo {
let x: Int
let y: Bool
}
MemoryLayout.size // returns 8 on 64-bit
MemoryLayout.size // returns 1
MemoryLayout.size // returns 9
MemoryLayout.stride // returns 16 because of alignment requirements
MemoryLayout.alignment // returns 8, addresses must be multiples of 8