If you have to serialize numbers, then I agree you shouldn’t use Int
or UInt
: they vary between architectures. That being said, you should never convert numbers just because it looks simpler: it hides potential overflow issues and other problems. Don’t assume Int
is Int32
or Int64
either: it can technically be anything.
By the way, Swift’s convention is to prefix anything that might produce undefined behavior with “unsafe”. Everything else is safe, even if only by virtue of a runtime crash. Your own code should follow that convention too, though you should avoid both if possible.
The reason this is relevant is that conversion between different integer types is by and large always safe: you don’t need to perform your own assertions, it’ll do that anyway. Same with overflow and integer operations: either it is guaranteed to check for it and respond by crashing (the “default”), or it is guaranteed to skip the check and wrap (the wrapping operators, which are therefore marginally faster).
That being said, don’t write any code that actually expects to fail an assertion or precondition. That’s a recipe for disaster, and also causes it to break if you want to disable the checks when compiling for extra efficiency.