简体   繁体   中英

How to get access to constants from separate .swift file

I'm learning swift and this seems like a simple problem yet I cannot figure it out. I'm making a trivia game to learn the basics and would like to store my question bank in a separate .swift file.

I can see the class come up in xcodes intellisense, but have no clue how to actually use the constants I have made in that file. The constants are of type dictionary and they contain the question, answers, correct answer, and question number within their key and value pairs.

In my main ViewController, I have an empty array I create and would like to populate it with the constant dictionary questions I have contained within my separate .swift file:

var BasicQuestionBank: [[String:String]] = [] // Array for multiple dictionary storage

func AddingToQuestionBankOne() {
        BasicQuestionBank.append(Questions.????????) // Can't figure out how to put this in array from separate file.
}

My separate .swift file looks simply like this but has multiple questions:

public class Questions {

    let question1 = [
        "question": "A square has how many sides?",
        "answerA": "1",
        "answerB": "2",
        "answerC": "3",
        "answerD": "4",
        "correctAnswer": "answerD",
        "questionNumber": "A-001-001"]
}

Also, to populate question banks arrays programmatically by the "questionNumber" key, is this possible? Say I have 20 questions with 10 being "A-001" prefix and 10 being "A-002" prefix, how would I choose to pick the 10 out of my choice? I have tried putting these in a for-in loop, but again, cannot figure out how to access this other .swift file.

Any help and insight would be greatly appreciated.

In your example, you've defined a class, Questions , that has a constant member variable. However, you need to create an "instance" of that class in order to access its member variable:

public class Questions {
    let question1 = [ etc. ]
}

var basicQuestionBank: [[String:String]] = []

func addToQuestionBankOne() {
    // qs is an instance of the Questions class
    let qs = Questions()
    // and has a member variable, question1
    basicQuestionBank.append(qs.question1)
}

Classes are like templates. You create instances of classes (these are called objects) that all have the same member variables and methods, but that can hold different values in their member variables.

In this case, it might seem a bit confusing, because you've created a class that has only one member, which is a let (so it can never hold anything other than its initial value), and no init method (which is the only place you could set question1 to be anything different than the default value you've given it). But nonetheless, you can only access that value by creating an instance of Questions , then accessing the question1 property on that instance.

By the way, for your next step you might want to try representing your questions as a struct, rather than a dictionary of strings:

struct QandA {
    let question: String
    let answers: [String]
    let correctAnswer: Int
    let questionNumber: String
}

let question1 = QandA(
    question: "A square has how many sides?",
    answers: ["One","Two","Three","Four"],
    correctAnswer: 4,
    questionNumber: "A-001-001")

Putting your data in a structure like this makes it much easier to operate on:

println(question1.question)
for (number, answer) in enumerate(question1.answers) {
    println("\(number): \(answer)")
}

Just like above, this is creating an instance of the QandA struct (structs are similar to classes – the Swift book has more details on the differences), called question1 , then accessing the properties of this instance.

You can enable Core Data in your project, then create a new Core Data object called "Question". Add fields for question, answers, correct answer, etc. Then, in AppDelegate, create the questions and add them to the ManagedObjectContext first time that app loads. You can find a good tutorial for Core Data in Swift here and here . If you ever want to change the questions, just change them in AppDelegate.swift and have the app reload them the next time it opens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM