可以将Any转换为Optional吗?

时间:2015-03-15 12:54:39

标签: swift casting optional

假设我有一段这样的代码:

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

现在我想将y转换为Int?:

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

这是不可能还是有另一种方式?

5 个答案:

答案 0 :(得分:7)

对于Swift 2.0,您可以使用以下内容:

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

或作为一项功能:

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)

如果你不喜欢反思,那么你可以这样做:

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

答案 1 :(得分:0)

您可以执行此操作以在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
}

所以在你的情况下,

let z = unwrap(y) as? Int

参考:How to unwrap an optional value from Any type?

答案 2 :(得分:0)

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 :(得分:0)

这个解决方案怎么样,我做了以前答案的通用版本。

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)

以下是Playground上的代码。

enter image description here

答案 4 :(得分:0)

如果有人正在寻找即插即用的Dictionary解决方案,则类似以下内容:

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)
    }
}