TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Binary operator “+” cannot be applied to operands of type “Int” and “Double”

Forums > Swift

Hey, I'm new to swift and I want to understand why it's impossible to do any operations with different types of variables. for example:

I can do this:

var number1 = 10 var number2 = 3.5 var number3 = 10 * 3.5 // 35

But I can't do this:

var number4 = number1 + number2

// Binary operator “+” cannot be applied to operands of type “Int” and “Double”

I understand that I can only multiple two of the same data type, but why?

I know that I should do this:

var number4 = Double(number1) + number2

I'm a very meticulous and I want to understand why it's not allowed in the Swift.

Thank you.

2      

Welcome to HackingWithSwift forums.

The excellent 100 Days of SwiftUI will take you on a terrific path to learn how to program applications using Swift and SwiftUI.

The first 10 days covers many basic concepts. You're asking about data types, specifically integers and doubles. Swift is very meticulous when determining what data types you are using, calculating, and displaying.

@twoStraws has an excellent lesson explaining this. I don't want to copy and paste his lesson here to answer your question. So, please review it, try the examples, take the quiz, and return here if you need addiional clarification.

See -> Integer and Double Data Types

3      

var number4 = number1 + number2

// Binary operator "+" cannot be applied to operands of type "Int" and "Double"

I understand that I can only multiple two of the same data type, but why?

Think about it like this:

If you have, say, 4 apples and you add 3 apples, you end up with 7 apples.

If you have 4 oranges and you add 3 oranges, you end up with 7 oranges.

If you have 4 apples and you add 3 oranges, you end up with 7... what?

Same with Double and Int. What type would you end up with by adding them together?

2      

@roosterboy )😂😂😂 this was hilarious analogy.

If you have 4 apples and you add 3 oranges, you end up with 7... what?

Simple answer, you end up with Fruit salad! )))

PS: No offence intended to the person for asking that question.

2      

Love @roosterboy answer.

As Swift is a Type Safe laugauge

You would not add a Int and String EG

let number1 = 10
let number2 = "3"

let answer = number1 + number2

However you can still add them by type casting the string

let answer = number1 + (Int(number2) ?? 0)

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.