DEV Community

CobraDJC
CobraDJC

Posted on

Question bot 2

In this lesson we are talking about the chat bot, similar to the question bot except instead of taking a question and returning an answer, this one just records a conversation like the messages app. Important things to note is that a scrolling list of items is known as a table view. Each item in a list is called a cell. In this lesson most of the app is already built, we will be working on the part of the app that keeps track of the conversation. The conversation is going to hold two types of messages, questions asked by the user and answers given by the brain of the app. The message struct holds the info needed to make an entry in the conversation: the date, the message text, and the type of message. The question answerer struct is the brain of the chat and is responsible for providing answers to questions. The conversation data source class is responsible for holding and updating the details of the current conversation. Currently conversation data source doesn’t do anything when given orders and doesn’t give any helpful answers when asked questions. When you type a question, the app will think a little bit, then revert to what it looked like before. Nothing changes because the data source is always saying there are no messages in the conversation. When you change the message count property to have a value of 1 instead of 0, you can see a single message in the conversation. If you ask a question, you’ll see that nothing changes, but you still see the console messages about adding question-and-answer messages. What should happen is that each time a message is added to the conversation, the data source should update the number of messages, so the next time it’s asked, it can give the correct answer. By changing the definition of the message count property to var instead of let, setting the initial value back to 0, and adding the line of code messageCounter += 1, we can increase the message counter property each time a message is added to the conversation. You can see that the right number of messages is displayed but they’re all the same answer. A remainder operator is used to determine if the message should be a question or an answer then it creates and returns something appropriate. All that’s missing now is to be able to see the actual contents of the conversation. The conversation data source needs a way to store the questions that the user has entered and the answers given by the brain. You can do this using an array of messages, starting with a blank one, which will store the questions and answers.

Top comments (0)