Unit 16: Question Bot 2 Flashcards

1
Q

Cell

A

in a table view, it displays a single item from the data collection

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Data Source

A

used via table view to find out what information it should display and how it should display it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Table View

A

a type of UI that displays several items or records in rows and columns on the screen

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Additional Notes

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

A scrolling list of items is known as a __________

A

Table view

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

To create a question, please write this:

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Working with Indexes

A

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) 
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Exploring the Project

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Assets.xcassets

A

The asset catalog holding all of the images used in ​

the app.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

ConversationViewController.swift

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Model

A

Details are provided in subsequent cards

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

AppDelegate.swift

A

Part of the standard app template, normally used ​

to handle events such as the app being launched.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Info.plist

A

Part of the standard app template, holding information ​

about the app itself.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

The main Swift file inside the Model Group

A

Message.swift

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Message.swift

A

The app will show you a conversation between you and a robot. This file describes the things that can make up a conversation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

The conversation is going to hold two types of messages

A
questions ​
(asked by the user of the app) 
& 
answers
(given by the brain of the app)
17
Q

The MessageType enum holds the two possible values

A

.question and .answer

18
Q

Adding Storage

A

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.

19
Q

How to do so…

A

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.

20
Q

Refinements

A

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.