Search code examples
swifttypesintegerswift5

Why an Int32 variable can't be assigned to an Int64 variable or vice versa in Swift?


I could not assign Int32 var/let to Int64 var/let or vice-versa in Swift. I get a compile time error when I attempt to do so. What is the reason behind this ?


Solution

  • If you declare a variable as Int32, the memory allocated for that variable is 4bytes and for Int64 type variable the memory allocated is 8 bytes.

    You can't put 64bytes data into 32 byte data as you can't put 2litres of water into 1 litre bottle.

    In order to avoid such problems, swift has strict type checking feature and ensure such problems are not arise in run time.