WidgetKit 适当的占位符
WidgetKit proper placeholders
我在为 WidgetKit 找出合适的占位符时遇到了麻烦。我遇到的问题是我不确定如何为 Codable
数据设置默认值,我尝试过但它没有在我的占位符视图中显示任何数据。我希望我的占位符显示每小时和每天 Codable
数据,但使用默认值。
然后这是我的小部件中的占位符
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date(), weather: Weather(current: Current(symbol: "n200", symbolPhrase: "partly cloudy", windSpeed: 0)), hourly: Hourly(forecast: [Hour]()), daily: Daily(forecast: [Day]()))
}
struct Day: Codable {
let date: String
let symbol: String
let symbolPhrase: String
let maxWindSpeed: Int
enum CodingKeys: String, CodingKey {
case date, symbol, symbolPhrase, maxWindSpeed
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
date = try container.decode(String?.self, forKey: .date) ?? "2020-03-16"
symbol = try container.decode(String?.self, forKey: .symbol) ?? "d320"
symbolPhrase = try container.decode(String?.self, forKey: .symbolPhrase) ?? "partly cloudy"
maxWindSpeed = try container.decode(Int?.self, forKey: .maxWindSpeed) ?? 0
}
}
简单的解决方案,出于某种原因它让我头疼。
Array(repeating: Hour(time: "2020-03-16T14:00+01:00", symbol: "n200" , symbolPhrase: "partly cloudy", windSpeed: 0), count: 5)
我在为 WidgetKit 找出合适的占位符时遇到了麻烦。我遇到的问题是我不确定如何为 Codable
数据设置默认值,我尝试过但它没有在我的占位符视图中显示任何数据。我希望我的占位符显示每小时和每天 Codable
数据,但使用默认值。
然后这是我的小部件中的占位符
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date(), weather: Weather(current: Current(symbol: "n200", symbolPhrase: "partly cloudy", windSpeed: 0)), hourly: Hourly(forecast: [Hour]()), daily: Daily(forecast: [Day]()))
}
struct Day: Codable {
let date: String
let symbol: String
let symbolPhrase: String
let maxWindSpeed: Int
enum CodingKeys: String, CodingKey {
case date, symbol, symbolPhrase, maxWindSpeed
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
date = try container.decode(String?.self, forKey: .date) ?? "2020-03-16"
symbol = try container.decode(String?.self, forKey: .symbol) ?? "d320"
symbolPhrase = try container.decode(String?.self, forKey: .symbolPhrase) ?? "partly cloudy"
maxWindSpeed = try container.decode(Int?.self, forKey: .maxWindSpeed) ?? 0
}
}
简单的解决方案,出于某种原因它让我头疼。
Array(repeating: Hour(time: "2020-03-16T14:00+01:00", symbol: "n200" , symbolPhrase: "partly cloudy", windSpeed: 0), count: 5)