Rob
Rob

Reputation: 437622

How to write Dictionary extension that handles optional values

I'm trying to implement a Dictionary extension and I want to handle optional values. But whatever I do, if I use my method on a [String: String?] dictionary, it fails to optionally bind the value. How do you write an extension to a dictionary that gracefully handles optional values?


Consider the following extension:

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

So consider the following code:

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

And it curiously reports

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

I can write a non-extension method that handles dictionary parameters with optional values, but don't see how to do it as an extension of Dictionary.

Upvotes: 10

Views: 1214

Answers (2)

Ben Kane
Ben Kane

Reputation: 10040

You could do this with reflection. Doesn't require much more code than you already have:

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

Returns

foo = bar

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

Returns

foo = nil cannot be cast to `String`

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

Returns

foo = Optional(<UIViewController: 0x7fee7e819870>) cannot be cast to `String`

Upvotes: 4

Tony Swiftguy
Tony Swiftguy

Reputation: 444

I get no problems when I insert ':String?' right after valueString as shown below:

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

Upvotes: 0

Related Questions