我可以从Dictionary <string,anyobject>?</string,anyobject>返回类型化对象

时间:2014-11-07 06:10:02

标签: swift types

这就是我现在所拥有的:

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 typed
func 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
    }
}

我事先知道字典中的键和特定值类型。我希望能够做的是将一个类型与每个键相关联,这样当我访问字典的内容时我可以有类型的返回值。

0 个答案:

没有答案