New to Swift - Need help

This is probably going to be for elementary for most but I'm new to Swift and trying to do a chatbot in Xcode where response is dependent on keywords and without the same response for 5 interactions. I want to increase the answer under each category each time and reset it after the 5 responses to start over. I have tried numerous ways and keep getting errors. Here is my code below for the questionanswer page. Can anyone assist?

Patricia Kennison
5:11 PM (31 minutes ago)
to me

struct QuestionAnswerer {

/// Creates a String in response to another String.

func responseTo(question: String) -> String {

    let lowerQuestion = question.lowercased()


    //class Counter {

    //var count = 0

    //func increment () {

    //count += 1

    // }


//let counter = Counter()


    //func increment (by amount: Int) {

    //count += amount


    if lowerQuestion.hasPrefix("hello") {

        return "hello there!"

    } else if lowerQuestion == "where are the cookies?" {

        return "I ate them!"

    } else if lowerQuestion.hasPrefix("where") {

        return "To the beach or mountains!"

    } else if lowerQuestion.contains("stressed") {

        let stressedNumber = question.count % 3

        switch stressedNumber {

            case 0:

            return "Please take a deep breath and exhale slowly"

            case 1:

            return "Try taking a walk. It can really help clear your head"

            case 2:

            return "Listen to your favorite music"

            case 3:

            return "Make a list of what is bringing on your stress"


            return "If you can get some exercise in today, that should help!"




        } else if lowerQuestion.contains("depressed") {

        let depressedNumber = question.count % 3

        switch depressedNumber {

        case 0:

            return "Maybe you could hang out with your friends to help"

        case 1:

            return "Taking a trip might help you"

        case 2:

            return "Some uplifting music may lift your spirits!"

        case 3:

            return "Try a board game if you have someone to play it with you."


        return "I am sorry to hear that! Take your mind to your happy place!"



    } else {

        let defaultNumber = question.count % 3

        if defaultNumber == 0 {

            return "That really depends"

        } else if defaultNumber == 1 {

            return "I think so, yes"

        } else if defaultNumber == 2 {

            return "I hope tomorrow is better for you!"

        } else if defaultNumber == 3 {

            return "Ask me again tomorrow"

        } else {

            return "Try to stay busy to keep your mind off things!"





The first thing I'd note here is that question.count is going to give you the number of characters (e.g. letters/numbers/punctuation) in the question string, and not the number of times it's been asked.

There are lots of possible approaches to keeping track of the count. Probably the most simple is to move your variables (e.g. depressedNumber, stressedNumber) outside of the function to the containing object as vars. The idea here is that variables defined within a function only last within the 'scope' of the function; as soon as you return they no longer exist. Because of this, you need to store them on something that lasts longer, like on the enclosing class/struct or as global variables.

Then, you could do something like:

if lowerQuestion.contains("stressed") {
        let responseIndex = self.stressedNumber
        self.stressedNumber = (self.stressedNumber + 1) % 3

        switch responseIndex { 

Awesome! Thank you! I will try that! I appreciate the help!

Hi, just found this and I have been trying to find a way to build a chatbot in swift for a very long time and this is exactly what I have been trying to do. Would it be possible if you could help me get started.