Is it possible to cast Any to an Optional?

2020-02-07 01:25发布

问题:

Let's say I have a piece of code like this:

let x: Int? = 10  
let y: Any = x

Now I want to cast y to Int?:

let z = y as Int? // Error: Cannot downcast from 'Any' to a more optional type 'Int?'

Is this just not possible or is there another way?

回答1:

For Swift 2.0, you can use the following:

let x: Int? = 10
let y: Any = x
let z = Mirror(reflecting: y).descendant("Some") as? Int

Or as a function:

func castToOptional<T>(x: Any) -> T? {
    return Mirror(reflecting: x).descendant("Some") as? T
}
let x: Int? = 10
let y: Any = x
let z: Int? = castToOptional(y)

Or you can do this if you don't like Reflection:

func castToOptional<T>(x: Any) -> T {
    return x as! T
}
let x: Int? = 10
let y: Any = x
let z: Int? = castToOptional(y)


回答2:

func castAsOptionalInt(value: Any)->Int? {
    let mirror = Mirror(reflecting:value)
    if mirror.subjectType == Optional<Int>.self {
        let ret = mirror.children.first?.1
        return ret as? Int
    } else {
        return nil
    }
}

let x: Int? = 10
let y: Any = x
let z = castAsOptionalInt(y) // 10
let a: Double? = 10
let b: Any = a
let c = castAsOptionalInt(b) // nil


回答3:

You can do this to get an optional out on Any

func unwrap(any:Any) -> Any? {
    let mi:MirrorType = reflect(any)
    if mi.disposition != .Optional {
        return any
    }
    if mi.count == 0 { return nil } // Optional.None
    let (name,some) = mi[0]
    return some.value
}

So in your case,

let z = unwrap(y) as? Int

Reference : How to unwrap an optional value from Any type?



回答4:

how about this solution, I made a generic version of previous answer.

fileprivate func unwrap<T>(value: Any)
  -> (unwraped:T?, isOriginalType:Bool) {

  let mirror = Mirror(reflecting: value)
  let isOrgType = mirror.subjectType == Optional<T>.self
  if mirror.displayStyle != .optional {
    return (value as? T, isOrgType)
  }
  guard let firstChild = mirror.children.first else {
    return (nil, isOrgType)
  }
  return (firstChild.value as? T, isOrgType)
}

let value: [Int]? = [0]
let value2: [Int]? = nil

let anyValue: Any = value
let anyValue2: Any = value2

let unwrappedResult:([Int]?, Bool)
  = unwrap(value: anyValue)    // ({[0]}, .1 true)
let unwrappedResult2:([Int]?, Bool)
  = unwrap(value: anyValue2)  // (nil, .1 true)
let unwrappedResult3:([UInt]?, Bool)
  = unwrap(value: anyValue)  // (nil, .1 false)
let unwrappedResult4:([NSNumber]?, Bool)
  = unwrap(value: anyValue)  ({[0]}, .1 false)

The following is code on Playground.



回答5:

In case anyone's looking for a plug-and-play Dictionary solution, something like this:

extension Dictionary where Value == Any {
    func typedValue<T>(forKey key: Key) -> T? {
        if let anyValue: Any = self[key] {
            return (anyValue as! T)
        }
        return nil
    }

    func optionalTypedValue<T>(forKey key: Key) -> T?? {
        return self.typedValue(forKey: key)
    }
}