Swift - Data Type Integer Operations

Introduction

When you try to add two numbers of different integer types, you will get an error.

Consider the following example:

var i1: UInt8 = 255
var i2: UInt16 = 255
var i3 = i1 + i2      //cannot add two variables of different types

To fix this, you need to typecast one of the types to be the same as the other type:

var i3 = UInt16 (i1)  + i2  //i3 is now UInt16

Related Topic