How to Typecast Obligation is Apple Swift?

Total Post:135

iPhone  Ios 
 1029  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 
  1. Post:119

    Re: How to Typecast Obligation is Apple Swift?

    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.

      Modified On Apr-06-2018 12:55:22 AM