在Swift中遇到NSDictionary函数问题

时间:2014-06-13 10:36:28

标签: objective-c nsdictionary swift

我有以下imageMap在名为NSDictionary的{​​{1}}中返回Class

WXCondition

然后我有以下方法返回+ (NSDictionary *)imageMap { // 1 static NSDictionary *_imageMap = nil; if (! _imageMap) { // 2 _imageMap = @{ @"01d" : @"weather-clear", @"02d" : @"weather-few", @"03d" : @"weather-few", @"04d" : @"weather-broken", @"09d" : @"weather-shower", @"10d" : @"weather-rain", @"11d" : @"weather-tstorm", @"13d" : @"weather-snow", @"50d" : @"weather-mist", @"01n" : @"weather-moon", @"02n" : @"weather-few-night", @"03n" : @"weather-few-night", @"04n" : @"weather-broken", @"09n" : @"weather-shower", @"10n" : @"weather-rain-night", @"11n" : @"weather-tstorm", @"13n" : @"weather-snow", @"50n" : @"weather-mist", }; } return _imageMap; } 方法:

imageMap

我正在尝试将这些方法转换为Swift,到目前为止,我有以下内容:

- (NSString *)imageName {
    return [WXCondition imageMap][self.icon];
}

不太确定如何正确返回func JSONKeyPathsByPropertyKey() -> NSDictionary { return { "date": "dt", "locationName": "name", "humidity": "main.humidity", "temperature": "main.temp", "tempHigh": "main.temp_max", "tempLow": "main.temp_min", "sunrise": "sys.sunrise", "sunset": "sys.sunset", "conditionDescription": "weather.description", "condition": "weather.main", "icon": "weather.icon", "windBearing": "wind.deg", "windSpeed": "wind.speed" } } func imageName() -> NSString { return "" //[WXCondition imageMap][self.icon]; }

2 个答案:

答案 0 :(得分:2)

+方法称为class方法。在Swift中,您使用关键字class。如果您需要NSDictionary,则可以创建普通字典,然后只使用as关键字(您也可以将其桥接到目标c,但这更明确)。所以这就是你最终会得到的:

class func blah() -> NSDictionary {
    return [
        "date": "dt",
        "locationName": "name"
        // ...
    ] as NSDictionary
}

您的第二个功能(虽然我没有看到太多信息)可能只是修改为:return WXCondition.imageMap()[self.icon](您可能需要包含as NSString,具体取决于信息的来源来自和你试图用它做什么)

答案 1 :(得分:1)

试试这个:

class A {
    class func myDict() -> NSDictionary {
        var occupations = [
            "Malcolm": "Captain",
            "Kaylee": "Mechanic",
        ]         
        return NSDictionary(dictionary: occupations)
    }
}
println("Test = \(A.myDict())")