Book Image

Swift Cookbook

Book Image

Swift Cookbook

Overview of this book

Table of Contents (18 chapters)
Swift Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating an exam app


In this recipe, we will create an exam app. For this exam, we will choose some random questions and the user will answer them. At the end, the app will show the user score and start again with a new exam.

Getting ready

First, open Xcode and create a project called Chapter 3 Examination, then create a file called question.swift. This is where we will define a question for an exam.

How to do it...

To create an exam app, follow these steps:

  1. Open the storyboard and add a label and three buttons to the view controller. You will have something similar to the following screenshot:

  2. Copy the following code into the question.swift file:

    struct Question {
        var question:String
        var answer1:String
        var answer2:String
        var answer3:String
        var rightAnswer:Int
        var userAnswer:Int?
        
        init(question:String, answer1:String, answer2:String, answer3:String, rightAnswer:Int){
           self.question = question
            self.answer1 = answer1
            self.answer2 = answer2
      ...