10

我正在尝试实现Dictionary扩展,并且我想处理可选值。但是无论我做什么,如果我在字典上使用我的方法[String: String?],它就无法选择性地绑定值。你如何为一个优雅地处理可选值的字典编写一个扩展?


考虑以下扩展:

extension Dictionary {
    func someMethod() {
        for (key, value) in self {
            if let valueString = value as? String {
                println("  \(key) = \(valueString)")
            } else {
                println("  \(key) = \(value) cannot be cast to `String`")
            }
        }
    }
}

所以考虑下面的代码:

let dictionary: [String: AnyObject?] = ["foo": "bar"]
dictionary.someMethod()

它奇怪地报告

foo = Optional(bar) cannot be cast to `String`

我可以编写一个非扩展方法来处理具有可选值的字典参数,但不知道如何将其作为Dictionary.

4

2 回答 2

4

你可以通过反射来做到这一点。不需要比您已经拥有的更多的代码:

extension Dictionary
{
    func someMethod()
    {
        for (key, value) in self
        {
            var valueRef = _reflect(value)

            while valueRef.disposition == .Optional && valueRef.count > 0 && valueRef[0].0 == "Some"
            {
                valueRef = valueRef[0].1
            }

            if let valueString: String = valueRef.value as? String
            {
                print("  \(key) = \(valueString)")
            }
            else
            {
                print("  \(key) = \(value) cannot be cast to `String`")
            }
        }
    }
}

let dictionary: [String : AnyObject?] = ["foo" : "bar"]
dictionary.someMethod()

退货

foo = bar

let dictionary: [String : AnyObject?] = ["foo" : nil]
dictionary.someMethod()

退货

foo = nil cannot be cast to `String`

let dictionary: [String : AnyObject?] = ["foo" : UIViewController()]
dictionary.someMethod()

退货

foo = Optional(<UIViewController: 0x7fee7e819870>) cannot be cast to `String`
于 2015-02-05T20:01:16.957 回答
0

插入 ':String?' 时我没有遇到任何问题 就在 valueString 之后,如下所示:

extension Dictionary {
    func someMethod() -> Bool {
        for (key, value) in self {
            if let valueString:String? = value as? String {
                println("  \(key) = \(valueString)")
            } else {
                println("  \(key) = \(value) cannot be cast to `String`")
                return false
            }
        }
        return true
    }
}

func doSomething() {
    let dictionary: [String: AnyObject?] = ["foo": "bar"]
    if dictionary.someMethod() {
        println("no problems")
    } else {
        println("casting issues")
    }
}
于 2015-01-06T02:39:08.423 回答