HOW TO TYPECAST OBLIGATION IS APPLE SWIFT?

Chintoo Semi

Total Post:135

Points:947
Posted by  Chintoo Semi
iPhone  Ios 
 809  View(s)
Ratings:
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. Goti Bandu

    Post:119

    Points:835
    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.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!