UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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 Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free 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.