Reputation: 60039
How would one detect if a client system is 32 or 64 bit in Swift?
I wasn't able to find anything in documentation and I had a hard time finding a solution. Therefore posting the answer below.
Upvotes: 4
Views: 2080
Reputation: 51911
The size of Int
is guaranteed to be the same as platform’s native word size. see the docs.
So this should works:
let bit = sizeof(Int) * Int(BYTE_SIZE)
let is64bit = sizeof(Int) == sizeof(Int64)
let is32bit = sizeof(Int) == sizeof(Int32)
Upvotes: 9
Reputation: 60039
In a 64-bit environment, a CGFloat
is the same size as a Double
. In a 32-bit environment it is the same size as a Float
. There's a constant CGFLOAT_IS_DOUBLE
which is therefore 1
on a 64-bit system.
let bit = 32 + (32 * CGFLOAT_IS_DOUBLE)
Upvotes: 3