gpt4 book ai didi

objective-c - Swift 中 NSDictionary 函数的问题

转载 作者:行者123 更新时间:2023-11-28 11:23:46 25 4
gpt4 key购买 nike

我有以下 imageMap,它在名为 WXConditionClass 中返回 NSDictionary:

+ (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 方法的方法:

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

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

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];
}

不太确定如何正确返回 NSDictionary

最佳答案

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

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

你的第二个函数(虽然我没有看到太多关于它的信息)可能只是被修改成类似的东西:return WXCondition.imageMap()[self.icon](你可能需要include as NSString 取决于信息的来源和你试图用它做什么)

关于objective-c - Swift 中 NSDictionary 函数的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24203345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com