Значение словаря, возвращающее ноль в Swift - PullRequest
0 голосов
/ 03 марта 2020

В этом коде я пытаюсь извлечь дни недели из строки. Я создал словарь с именем dayDictionary для более быстрого доступа к дням недели

dayDictionary заполнен идеально, но я не могу получить доступ к элементу, он всегда возвращает nil

словарь доступа к словарю snip равен

for word in components {
        //print(String (word))

        if previous == "to" {
            //print("Hi end")
            endIndex = dayDictionary[String (word)] ?? -1
            break
        }

        if word == "to" {
            //print("Hi Start")

            startIndex = dayDictionary[previous] ?? -1
            continue
        }

        if let validIndex = dayDictionary[String (word)] {
            duration += [validIndex]
            print("Valid")
        }

        previous = String (word)
    }

Вот полный код для лучшего понимания

func findWeekDays(sentence: String, weekdays: [String]) -> [Int] {
    var dayDictionary: [String : Int] = [:]

    for index in 0..<weekdays.count {
        dayDictionary[weekdays[index]] = index
    }

    print(dayDictionary)

    sentence.lowercased()
    let components = sentence.split{ !$0.isLetter }

    var duration: [Int] = []

    var startIndex: Int = -1
    var endIndex: Int = -1
    var previous: String = ""

    for word in components {
        //print(String (word))

        if previous == "to" {
            //print("Hi end")
            endIndex = dayDictionary[String (word)] ?? -1
            break
        }

        if word == "to" {
            //print("Hi Start")

            startIndex = dayDictionary[previous] ?? -1
            continue
        }

        if let validIndex = dayDictionary[String (word)] {
            duration += [validIndex]
            print("Valid")
        }

        previous = String (word)
    }

    //print("\(startIndex), \(endIndex)")

    if startIndex != -1 && endIndex != -1 {
        if startIndex > endIndex {
            for index in startIndex...6 {
                duration += [index]
            }

            for index in 0...endIndex {
                duration += [index]
            }
        }

    }

    duration.sort()

    if duration.count == 0 {
        duration += [0, 1, 2, 3, 4, 5, 6]
    }

    return duration
}

func userInput() {
    let weekdays: [String] = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]

    let userSting: String = "Set alarm Monday to Friday"

    let dayIndex: [Int] = findWeekDays(sentence: userSting, weekdays: weekdays)

    for index in dayIndex {
        print("\(weekdays[index])")
    }
}

userInput()

1 Ответ

1 голос
/ 03 марта 2020

Вы должны использовать dayDictionary[word.description.lowercased()] вместо dayDictionary[String(word)]

Я пробовал на игровой площадке и добавил несколько print строк для l oop ->

for word in components {
    print("word is: \(word)")
    print("components is: \(components)")
    print("previous is: \(previous)")
    print("dayDictionary is: \(dayDictionary)")
    print("item in dayDict is: \(dayDictionary[word.description.lowercased()])")

    if previous == "to" {
        print("Hi end")
        endIndex = dayDictionary[String(word)] ?? -1
        break
    }

    if word == "to" {
        print("Hi Start")

        startIndex = dayDictionary[previous] ?? -1
        continue
    }

    if let validIndex = dayDictionary[word.description.lowercased()] {
        duration += [validIndex]
        print("Valid")
    }

    previous = String(word)
    print("---------------------------------------")
}

Тогда вывод:

1st iteration

word is: Set
components is: ["Set", "alarm", "Monday", "to", "Friday"]
previous is: 
dayDictionary is: ["tuesday": 1, "wednesday": 2, "saturday": 5, "monday": 0, "thursday": 3, "sunday": 6, "friday": 4]
item in dayDict is: nil
---------------------------------------

2nd iteration

word is: alarm
components is: ["Set", "alarm", "Monday", "to", "Friday"]
previous is: Set
dayDictionary is: ["tuesday": 1, "wednesday": 2, "saturday": 5, "monday": 0, "thursday": 3, "sunday": 6, "friday": 4]
item in dayDict is: nil
---------------------------------------

3rd iteration

word is: Monday
components is: ["Set", "alarm", "Monday", "to", "Friday"]
previous is: alarm
dayDictionary is: ["tuesday": 1, "wednesday": 2, "saturday": 5, "monday": 0, "thursday": 3, "sunday": 6, "friday": 4]
item in dayDict is: Optional(0)
Valid
---------------------------------------

4th iteration

word is: to
components is: ["Set", "alarm", "Monday", "to", "Friday"]
previous is: Monday
dayDictionary is: ["tuesday": 1, "wednesday": 2, "saturday": 5, "monday": 0, "thursday": 3, "sunday": 6, "friday": 4]
item in dayDict is: nil
Hi Start

5th iteration

word is: Friday
components is: ["Set", "alarm", "Monday", "to", "Friday"]
previous is: Monday
dayDictionary is: ["tuesday": 1, "wednesday": 2, "saturday": 5, "monday": 0, "thursday": 3, "sunday": 6, "friday": 4]
item in dayDict is: Optional(4)
Valid
---------------------------------------

Пожалуйста, посмотрите на третью и пятую итерации. Мы могли бы получить предмет от dayDictionary.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...