How do I return the original value from any in Swift?

I am facing a serious problem. This is understandable as far as I know.

    let a1:Int?     =   11223344
    let a2:Any      =   a1
    let a3:Int?     =   a2 as? Int

    println(a3)

    // result: nil

      

Why is this happening with Any

? How do I get the original value back from Any

?

I am using Xcode 6.0.1.

+3


source to share


1 answer


You declared a1

as Optional

, which is a type enum

, and then assign the optional (enum) value a2

. Note that the enumeration is not Int

, so attempting to dynamically migrate a2

to Int

using as?

fails. As a result, it is a3

set to nil

.

To fix this, you can explicitly expand the optional value, for example:



    let a2:Any =  a1!

      

+3


source







All Articles