0

I'm very new to programming and I'm trying to learn the basics of Swift.

I have coded a very simple quiz game for iOS whereby the app moves through the questions in a sequential order. I am wanting to move onto a simple text-adventure game. To start off with I changed the quiz questions to scenarios which you can play through. However, I would now like to have it where you can click either of the two buttons displayed, one button would move you on to one scenario, and the other button on to a different scenario.

Below is the code I have so far, whereby it follows the simple sequential order of scenarios presented to the user where one answer allows you to move on, and one answer doesn't. What would be the best way to implement a structure whereby all the scenarios and answers are stored and the user can navigate through the story by selecting different answers to different scenarios.

import UIKit

struct Scenario {
    var Scenario : String!
    var Answers : [String]!
    var Answer : Int!
}

class ViewController: UIViewController {
    @IBOutlet var actionButton: [UIButton]!
    @IBOutlet var scenarioLabel: UILabel!

    var Scenarios = [Scenario]()
    var sNumber = Int()
    var answerNumber = Int()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.


        listScenarios()
        pickScenario()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func listScenarios() {

        Scenarios = [Scenario(Scenario: "HELLO? Someone please answer me, I NEED HELP!", Answers: ["Who are you? I can help you.","*** Ignore ***"], Answer: 0),
                     Scenario(Scenario: "That is something I cannot share with you", Answers: ["Why?","I am unwilling to help if you don't share this with me."], Answer: 1),
                     Scenario(Scenario: "I... I... I just can't. You have to understand", Answers: ["Okay, I'll help.","What has happened to you?"], Answer: 0),]

    }

    func pickScenario() {
        if Scenarios.count > 0 {
            sNumber = 0
            scenarioLabel.text = Scenarios[sNumber].Scenario

            answerNumber = Scenarios[sNumber].Answer

            for  i in 0..<actionButton.count {

                actionButton[i].setTitle(Scenarios[sNumber].Answers[i], forState: UIControlState.Normal)

            }

            Scenarios.removeAtIndex(sNumber)
        }
        else {
            NSLog("Done!")
        }
    }

    @IBAction func button1(sender: AnyObject) {
        if answerNumber == 0 {
            pickScenario()
        }
        else{
            NSLog("Wrong!")
        }
    }

    @IBAction func button2(sender: AnyObject) {
        if answerNumber == 1 {
            pickScenario()
        }
        else{
            NSLog("Wrong!")
        }
    }
}
Casey Fleser
  • 5,707
  • 1
  • 32
  • 43
user2952907
  • 1
  • 1
  • 4
  • Are you looking to cache the answers temporarily, or store them persistently? – ZGski May 19 '16 at 14:14
  • Ideally, I would like to store all the scenarios + their corresponding answers (which are displayed on buttons) persistently. So that when upon the user selects a button corresponding to their answer, the next scenario + answers is loaded into the view. – user2952907 May 19 '16 at 14:17

1 Answers1

0

Since you're looking to store objects persistently, you actually have quite a few options. I've listed some popular choices below for you to take a look at and form an opinion on your own. Note that while the options I've listed are in no way complete or necessarily the best choices, but the ones that I've used successfully in past applications.

  • Core Data - Apple's native model management framework

  • FMDatabase - A third party SQLite wrapper

  • Realm - A third party replacement for SQLite & Core Data (My personal favorite)

The way you go about managing and saving your objects is completely up to you, and should be based on your application's needs and what you're comfortable with.

The UI you chose to display this data could be a series of tableViews, a graphical tree-hierarchy, or something else altogether. There are a number of ways you can go about displaying this data, but it's ultimately your decision.

ZGski
  • 2,398
  • 1
  • 21
  • 34