Q:

How two UInt8 variables are added in iOS Swift?

Say there are two variables:
let number1 : UInt8 = 100;
let number2 : UInt8 = 100;

You add and print them
print(number1 + number2)  //This prints 200

Now define one more
let number3 : UInt8 = 200;

And try to add now
print(number1 + number3)  // Throws execution was interrupted

I understand that the sum of number1 and number3 would be out of range of UInt8 but explicit casting also does not help, for example following line also gives the same error:
print(UInt8(number1 + number3)) // Throws execution was interrupted

The way I found was to do the following:
print(Int(number1) + Int(number3))

Is there a better way of adding UInt8 number when their sum goes out of range?

A:

Girish K Gupta,
UInt8 has max range 0 to 255. Which you can check using UInt8.min and UInt8.max. Basically 0 to 2 power 8.
Issue with print(number1 + number3) will return 300. 300 is greater then 255 hence crash.
When you add two UInt8 result will be by default casted to UInt8 hence the crash 
Finally when you Int(number1) + Int(number3) you are forcefully casting number1 and number3 to Int. 
When you use Int, range of its value depends on the platform on which you are running it either 32 bit or 64 bit. for example its range can be -2,147,483,648 to 2,147,483,647 for 32 bit.
When you add Int to Int result will be typecasted to Int. And believe me 300 is way inside the range :)
As per your question is there a better way to do it :)
Apple's docs clearly specifies and instructs to use Int rather then UInt8 or UInt32 or even UInt64 until and unless using UInt8, UInt32 or UInt64 is absolutely essential.
Here is the quote from apple's doc :)

“Use UInt only when you specifically need an unsigned integer type
  with the same size as the platform’s native word size. If this is not
  the case, Int is preferred, even when the values to be stored are
  known to be non-negative. A consistent use of Int for integer values
  aids code interoperability, avoids the need to convert between
  different number types, and matches integer type inference,”
Excerpt From: Apple Inc. “The Swift Programming Language (Swift 2.2).”
  iBooks. https://itun.es/in/jEUH0.l

So best thing for you to do :) follow apples instruction :) Change the number1,number2 and number3 to Int :) Problem solved :)
Hence no crash :)

A:

As you've said casting both UInt8 variables to Int overrides the default exception on overflow as the resulting Int now has room to fit the sum.
To avoid casting the variables for every operation we would like to overload the operator like this:
func + (left: UInt8, right: UInt8) -> Int {
   return Int(left) + Int(right)
}

However this will give us a compiler error as the + operator is already defined for adding two UInt8's.
What we could do instead is to define a custom operator, say ^+ to mean addition of two UInt8's but add them as Int's like so:
infix operator ^+ { associativity left precedence 140 }

func ^+ (left: UInt8, right: UInt8) -> Int {
    return Int(left) + Int(right)
}

Then we can use it in our algorithms:
print(number1 ^+ number3) // Prints 300

If you however want the result to just overflow you can use the overflow operators from the standard library:
print(number1 &+ number3) // Prints 44

