Initial prototype – Class creation and basic functionality.

In order to begin the development process on this project, the first thing that needed to be done was to establish the key functionality of the application at the most basic level, and begin figuring out how to best translate this into the Swift programming language. Following on from our discussion of prototyping strategies, this would enable the creation of an initial prototype which would serve both the purposes of allowing us to test our basic functionality and allowing the basis for an evolutionary prototype which will ultimately be turned into a final piece of software.

We went over our plans for the application, and determined the core functionality of our app to be :

  • Presenting a sound file to the user which can be played
  • Providing multiple options for the user to choose a phrase which relates to the sound
  • Determining whether the option they have selected is the correct answer
  • Moving to a fact/congratulations screen and then the next question after a question has been answered, updating the information on display

In order to more quickly begin prototyping of the application, and due to our current lack of knowledge on the best ways to work with sound in Swift/Xcode, we decided to develop an initial prototype application which focused on the last three points. This piece of software would therefore simply present a set of choices, and then progress through to additional groups of options after the pre-determined ‘correct’ option was chosen.

In order to comply with object-oriented programming practices, it was decided that the best way to structure this functionality was to implement a custom class for our questions. This could then be used, and passed different data, to create instances of question objects which could be stored and used to populate the information displayed on the screen.


class Question {
var soundFilePath: String
var options: [String]
var correctAnswer: Int
var fact: String

init(soundFilePath: String, options: [String], correctAnswer: Int, fact: String) {
self.soundFilePath = soundFilePath
self.options = options
self.correctAnswer = correctAnswer
self.fact = fact
}

This is the Question class that I ended up creating. It is quite simple, and it does not have any methods. This is because I intend to use this class as a structure for storing data for each question, but the functionality of the application surrounding the questions and progression will be handled by a main quiz controller, using Swift’s ViewControllers. The class stores five properties – a sound file path (although this is not yet being used), an array of options that make up the potential choices for each question, an integer value which stores which member of the options array is the correct one, and a fact string to store the interesting fact that will be displayed about each question after its completion (although this is also not currently being used).


func updateText(question: Question){
optionOneButton.setTitle(question.options[0], forState: .Normal)
optionTwoButton.setTitle(question.options[1], forState: .Normal)
optionThreeButton.setTitle(question.options[2], forState: .Normal)
optionFourButton.setTitle(question.options[3], forState: .Normal)

In the view controller for the quiz page, this function has been written to update the information shown on the screen when a new question is loaded. When the function is called, a variable object of the Question class, which will be set to the current question to be displayed, is accessed, and four buttons which I have laid out in the view (one for each option) have their title text set to the contents of the options array of the question object. This function will be called whenever the application needs to transition to a new question – so primarily when a question is answered correctly. It is also called upon initial load of the view to set the text for the first question.

 


var questions: [Question] {
return [
Question(soundFilePath: "", options: {
return ["Pillowblade",
"Softknife",
"Softsword",
"Floppystabber"]
}(), correctAnswer: 3, fact:""),
Question(soundFilePath: "", options: {
return ["Longspee",
"Other Stuff",
"Things",
"Choices"]
}(), correctAnswer: 1, fact:""),
Question(soundFilePath: "", options: {
return ["Wrong",
"Wrong",
"Wrong",
"Correct Answer"]
}(), correctAnswer: 4, fact:"")
]
}

I declared in the code an array of three question objects with some example data filled out. By having the code interact with the data by means of an array of questions, the current can easily be accessed and saved as the current index of the array. This allows for an easy method of transitioning between questions, as the previous update method can simply be called on the current index of the array, and the index can be incremented for each question in series.


@IBAction func optionOne(sender: AnyObject) {

       checkSuccess(1)
}

In order for the user to actually select an option, the buttons for each were given a function, via IBAction elements, which runs a function called checkSuccess and passes it the number corresponding to the relevant option. This checkSuccess function essentially simply checks the number passed in with the correct answer integer value stored in the current question object in the array. If the values are equal, the current question index is incremented by one and the next question in the array loaded and displayed (eventually there will be a step in between where a view will be shown to the user displaying a congratulatory message and interesting fact before loading the next question). For now, if the values do not match this is simply printed to the console, however in the final application a message needs to be displayed telling the user of the app to try again.

So, to recap – this initial prototype of our application presents the user with a set of four choices that make up a question in an array, determines which option the user chooses and whether it is correct, and then if it is loads and displays the next question’s options. As far as basic functionality goes in our application, this essentially covers it, minus actually playing the sound, and displaying interesting facts about the answers – both of which will be worked on in the coming days.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s