如果我知道当天,如何确定一周的接下来几天?
How to determine the next days of the week if I know the current day?
我创建了一个天气应用程序并为此使用了 API。 (我使用 OpenWeatherMap 和 API 作为 "Call 5 day / 3 hour forecast data")
我得到了我需要的所有信息。
然后我得到了一周所有天的数据。例如,星期一温度 22 度,星期二温度 12 度等。
我可以获取当前星期几并添加到字典中。
但我无法获取新的 dayName(星期几)。我需要做什么?
我需要帮助获取一周中接下来几天的名称及其索引。然后将键值添加到字典中。
// Indexed all days of the week Sun, Mon, Wed and a t.c
let setWeekDay = IndexSet([1, 2, 3, 4, 5, 6, 7])
//I detected current day of the weak and added to dictionary
var weakDay = [String:Int]()
let calendar = Calendar.current
var weekday = calendar.component(.weekday, from: Date())
let components = DateComponents(weekday: weekday)
var dayName = calendar.nextDate(after: Date(), matching: components, matchingPolicy: .nextTime)?.Days()
weakDay = [dayName!:weekday]
if let nextWeekday = setWeekDay.integerGreaterThan(weekday) {
weekday = nextWeekday
} else {
weekday = setWeekDay.first!
}
...
// I extended date formatter
extension Date{
func Days() -> String{
let dataFormatter = DateFormatter()
dataFormatter.dateStyle = .short
dataFormatter.dateFormat = "EEEE"
return dataFormatter.string(from: self)
}
}
//This is dictionary will be change key and value dynamically.
//I need got dictionary as:
let weakDay = [
"Sunday": 1,
"Monday": 2,
"Tuesday": 3,
"Wednesday": 4,
"Thursday": 5,
"Friday": 6,
"Saturday": 7,
]
感谢大家的帮助,但我自己找到了问题的答案!
我需要做的事情:
if let cell = tableView.dequeueReusableCell(withIdentifier: "daysId", for: indexPath) as? DaysTVCell {
var nameWeakAndIndex = [String:Int]()
var dateComponents = DateComponents()
dateComponents.setValue(1, for: .day);
var now = Date()
for index in 0...5 {
let tomorrow = Calendar.current.date(byAdding: dateComponents, to: now)
let detectDayIndex = index
nameWeakAndIndex = [now.Days():detectDayIndex]
now = tomorrow!
daysWeak.sort(by: { (nameWeakAndIndex[[=10=].dayOfTheWeak] ?? 6) > (nameWeakAndIndex[.dayOfTheWeak] ?? 6) })
}
let day = daysWeak[indexPath.row]
cell.configureCell(daysForecast: day)
return cell
我创建了一个天气应用程序并为此使用了 API。 (我使用 OpenWeatherMap 和 API 作为 "Call 5 day / 3 hour forecast data") 我得到了我需要的所有信息。
然后我得到了一周所有天的数据。例如,星期一温度 22 度,星期二温度 12 度等。
我可以获取当前星期几并添加到字典中。
但我无法获取新的 dayName(星期几)。我需要做什么? 我需要帮助获取一周中接下来几天的名称及其索引。然后将键值添加到字典中。
// Indexed all days of the week Sun, Mon, Wed and a t.c
let setWeekDay = IndexSet([1, 2, 3, 4, 5, 6, 7])
//I detected current day of the weak and added to dictionary
var weakDay = [String:Int]()
let calendar = Calendar.current
var weekday = calendar.component(.weekday, from: Date())
let components = DateComponents(weekday: weekday)
var dayName = calendar.nextDate(after: Date(), matching: components, matchingPolicy: .nextTime)?.Days()
weakDay = [dayName!:weekday]
if let nextWeekday = setWeekDay.integerGreaterThan(weekday) {
weekday = nextWeekday
} else {
weekday = setWeekDay.first!
}
...
// I extended date formatter
extension Date{
func Days() -> String{
let dataFormatter = DateFormatter()
dataFormatter.dateStyle = .short
dataFormatter.dateFormat = "EEEE"
return dataFormatter.string(from: self)
}
}
//This is dictionary will be change key and value dynamically.
//I need got dictionary as:
let weakDay = [
"Sunday": 1,
"Monday": 2,
"Tuesday": 3,
"Wednesday": 4,
"Thursday": 5,
"Friday": 6,
"Saturday": 7,
]
感谢大家的帮助,但我自己找到了问题的答案! 我需要做的事情:
if let cell = tableView.dequeueReusableCell(withIdentifier: "daysId", for: indexPath) as? DaysTVCell {
var nameWeakAndIndex = [String:Int]()
var dateComponents = DateComponents()
dateComponents.setValue(1, for: .day);
var now = Date()
for index in 0...5 {
let tomorrow = Calendar.current.date(byAdding: dateComponents, to: now)
let detectDayIndex = index
nameWeakAndIndex = [now.Days():detectDayIndex]
now = tomorrow!
daysWeak.sort(by: { (nameWeakAndIndex[[=10=].dayOfTheWeak] ?? 6) > (nameWeakAndIndex[.dayOfTheWeak] ?? 6) })
}
let day = daysWeak[indexPath.row]
cell.configureCell(daysForecast: day)
return cell