let a = 4let b: Int32 = 100let c: UInt8 = 12It seems that we cannot include or minus once the variables are blended types.

So what"s the an outcome of this problem?




You are watching: The sum of a and b

*

Swift disallows math operations between different numeric types. Writing:

let result = a + b - c // error...is one error. Uneven you have certain requirements, choose all the operands space non-negative, the dominance of thumb is to transform everything to the widest data type, in this situation Int:

let an outcome = a + Int(b) - Int(c) // 92

*

For a finish answer I would say

"Swift go not enable you to do math with various mathematical types. The expression a+b-c will certainly throw one error, so we require to actors all the worths to the very same type:

let result = a+Int(b)-Int(c)"

(As Code various says in their answer, you should "promote" values to the widest/richest form e.g. From smaller sized integer types to larger, from unsigned to signed, or from integer to floating allude if any kind of of the values space floating point.)


*

Thanks because that contributing solution to stack Overflow!

Please be certain to answer the question. Carry out details and share her research!

But avoid

Asking for help, clarification, or responding to various other answers.Making statements based on opinion; ago them increase with references or personal experience.

To learn more, check out our advice on writing great answers.


write-up Your prize Discard

By click “Post your Answer”, girlfriend agree to our terms of service, privacy policy and also cookie policy


Not the prize you're feather for? Browse various other questions tagged swift or asking your very own question.


*

site architecture / logo design © 2021 ridge Exchange Inc; user contributions license is granted under cc by-sa. Rev2021.11.5.40661




See more: How Many Years Is 2000 Hours To Years Converter, 2000 Hours To Years

Stack Overflow works finest with JavaScript allowed
*

your privacy

By clicking “Accept all cookies”, girlfriend agree ridge Exchange can store cookie on your machine and disclose information in accordance through our Cookie Policy.