【发布时间】:2019-03-01 09:18:48
【问题描述】:
我有这个枚举:
enum WeatherMapDisplayType {
case temperature
case wind
}
我可以这样声明一个变量吗?
let markerView: mode == .temperature ? WeatherMapItemTemp : WeatherMapItemWind = UIView.fromNib()
知道mode 是WeatherMapDisplayType 类型
我怎样才能优雅地处理这种情况?
编辑:
我希望能够做这样的事情:
let markerView: WeatherMapItem = UIView.fromNib()
markerView.frame = CGRect(x: 0, y: 0, width: 80, height: 30)
markerView.setupWeatherInformations(mode: self.currentDisplayMode, forecast: forecasts)
marker.iconView = markerView
以前我只有WeatherMapItem 类型。
然后我被要求添加另一个天气图项目,这就是为什么我现在有WeatherMapItemTemp 和WeatherMapItemWind(也对应于我的枚举显示类型)。
func setupWeatherInformations(forecast: Forecast.HourlyForecast)
这是我的自定义类中的一种方法,用于配置网点。
但是如果我从框架中初始化我的自定义视图,我将无法访问此方法,因为它是 UIView 类型的。
【问题讨论】:
-
这里最优雅的方式很可能是使用enums with associated values。 以适当类型作为关联值的markerView.
-
WeatherMapItemTemp是什么?还是WeatherMapItemWind? -
这是我自定义的 UIView 类。你能用示例写一个答案,以便我将其标记为已回答吗?
-
@Balanced 答案已更新,请检查
-
@Balanced 是的,您需要从 nib 加载更新的答案请检查
标签: swift enums variable-declaration ternary