Unit 16: Question Bot 2 Flashcards
Cell
in a table view, it displays a single item from the data collection
Data Source
used via table view to find out what information it should display and how it should display it
Table View
a type of UI that displays several items or records in rows and columns on the screen
Additional Notes
Contains a list of messages forming a conversation, messages entered by the user, a “thinking” indicator, and an entry area where the user can type a question
A scrolling list of items is known as a __________
Table view
To create a question, please write this:
Let question = Message(date: Date (), text: “Do you know the way to Cupertino?”, type: .question)
Please check out the diagram on pg. 99
MessageCount (property) should have a value of 1 instead of 0
E.g., let messageCount = 1
Add (question: )
Add (answer: )
messageCount += 1
Index 0 -> question
Index 1 -> answer
Working with Indexes
To show the other questions whilst the app is “thinking”, please replace it with this code:
If index % 2 == 0 {
return Message (date: Date (), text: "Question" \ (index / 2)", type: .question) } else { return Message (date: Date(), text: "Answer \ (index / 2)", type: .answer) }
Exploring the Project
The files that make up the app are collected into groups. You’ll only be working with the files in the Model group for this project. Feel free to explore the other files, but don’t worry if you see things you don’t understand. This project has been organized so you can concentrate just on the areas you’ve been learning about so far.
Here’s a summary of what the other files in the project are for, in the order they’re shown in the project navigator:
UI
Main.storyboard: The interface of the app, including the layout of
the screens.
LaunchScreen.storyboard: The screen displayed when the app is first launched—an empty white screen.
ThinkingCell.swift: A specialized cell for showing the app is thinking.
ConversationCell.swift: A specialized cell for showing a message in
the conversation.
AskCell.swift: A specialized cell for allowing the user to type in a question.
”
Assets.xcassets
The asset catalog holding all of the images used in
the app.
ConversationViewController.swift
The main view controller for the app. This class is responsible for the list view and handling updates when the user asks questions. It’s the most complicated file in the project, and contains a lot of code you haven’t learned about yet.
Model
Details are provided in subsequent cards
AppDelegate.swift
Part of the standard app template, normally used
to handle events such as the app being launched.
Info.plist
Part of the standard app template, holding information
about the app itself.
The main Swift file inside the Model Group
Message.swift
Message.swift
The app will show you a conversation between you and a robot. This file describes the things that can make up a conversation.
The conversation is going to hold two types of messages
questions (asked by the user of the app) & answers (given by the brain of the app)
The MessageType enum holds the two possible values
.question and .answer
Adding Storage
The conversation data source needs a way to store the questions that the user has entered and the answers given by the brain. The method of storage must be able to:
Store as many messages as required.
Keep the messages in a specific order.
Give back a message at a specific index.
How to do so…
You’ve already learned about something that can do all this. It’s called
an array. Add the following property to hold an array of messages:
var messages = Message
This is initialized as an empty array. Add this code to add(question:)
to create a new message and store it in the array:
let message = Message(date: Date(), text: question, type: .question)
messages.append(message)
Add similar code to add(answer:):
let message = Message(date: Date(), text: answer, type: .answer)
messages.append(message)
Now each time the data source is asked to add a question or an answer,
it will create a new Message and add it to the array.
Remove the conditional statement lines after the print statement in the messageAt(index:) and replace them with this:
return messages[index]
Build and run the app and ask some questions. You’ll see your conversation history is now kept and displayed in the list. You should see a screen similar to the one shown to the left, with your questions and answers.
Refinements
There’s no need for the separate messageCount any more. Delete the
lines from add(question:) and add(answer:) where you increase
the message count, then replace the property with a computed property:
var messageCount: Int {
return messages.count
}
In addition to the “Ask a question…” prompt, it’s nice for the app to welcome the user and ask them to get started. To do that, instead of the messages array being created empty, you can add a welcome message. Replace the
line where the messages array is initialized with this:
var messages = [openingLine]
This code creates an array with a single message in it, rather than an
empty array of messages. You don’t need to change anything else, since the messageCount is a computed property that will have the right answer.
From the Project Navigator, open the QuestionAnswerer.swift under the Model section. Change the logic of the responseTo(question: String) implementation to return your own answers to the users’ queries.