Quantcast
Viewing all articles
Browse latest Browse all 39

Can I return typed objects from a Dictionary?

Here's what I have right now:

enum TestKeys: String {    case login = "login"    case username = "username"}var testData: Dictionary<String,AnyObject>  = // a dictionary of known key/values// I want the return value from this function to be typedfunc getTestObject(key: TestKeys) -> AnyObject! {    if let obj: AnyObject = testData[key.rawValue] {        return obj    } else {        assertionFailure("We cannot find \(key.rawValue) in our testData")        return nil    }}

I know in advance the keys and specific value types in my dictionary. What I'd like to be able to do is associate a type with each key, so that I can have typed return values when I access the content of the dictionary.


Viewing all articles
Browse latest Browse all 39

Trending Articles