Как определить следующие дни недели, если я знаю текущий день? - PullRequest
0 голосов
/ 22 апреля 2019

Я создал приложение погоды и для него я использовал API. (Я использовал OpenWeatherMap и API как «Позвоните на 5-дневный / 3-часовой прогноз») Я получил всю необходимую информацию.

Тогда я получил данные за все дни недели. Например, температура понедельника 22 градуса, температура вторника 12 градусов и т. Д.

Я могу узнать текущий день недели и добавить в словарь.

Но я не могу получить новое имя дня (день недели). Что мне нужно сделать? Мне нужна помощь в получении названий следующих дней недели и их указателей. А затем добавьте значение ключа в словарь.

// 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,
                ]

1 Ответ

0 голосов
/ 23 апреля 2019

Спасибо всем за помощь, но я нашел ответ на свой вопрос сам!Что мне нужно было сделать:

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[$0.dayOfTheWeak] ?? 6) > (nameWeakAndIndex[$1.dayOfTheWeak] ?? 6) })
            }


            let day = daysWeak[indexPath.row]
            cell.configureCell(daysForecast: day)
            return cell
...