Home > DeveloperSection > Forums > How to Typecast Obligation is Apple Swift?
Chintoo Semi

Total Post:135

Points:947
Posted on    October-17-2014 11:07 PM

 iPhone iPhone  Ios 
Ratings:


 1 Reply(s)
 568  View(s)
Rate this:
Am I missing something or typecast is obligatory while adding numbers having different types:

var i:Int=5
var d:Double=6.0

var q:Double= i + d 
var w:Double= Double(i) + d 


Goti Bandu

Total Post:119

Points:835
Posted on    October-17-2014 11:09 PM

Swift doesn't provide automatic conversion between numeric types.

Normally you just perform conversions like you used in your question.

I don't recommend it, but you could do this:

func +(i:Int, d:Double) -> Double {
    return Double(i) + d
}

let i: Int = 1
let d: Double = 7.0
i + d
// result: 8.0
You would need to provide a lot of overrides to cover all the bases. Like I said, I don't recommend it.

Don't want to miss updates? Please click the below button!

Follow MindStick