簡體   English   中英

Swift:擴展幫助

[英]Swift: Help on Extensions

class quizController: UIViewController {

    @IBOutlet weak var questionLabel: UILabel!

    @IBOutlet weak var button1: UIButton!

    @IBOutlet weak var button2: UIButton!

    @IBOutlet weak var button3: UIButton!

    @IBOutlet weak var button4: UIButton!


    var question : String
    var options : [String]
    var correctAns : Int

    init() {

        question = "What quiz are you taking?"
        options = ["Medical", "Bollywood", "Math", "Trivia"]
        correctAns = 0



    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

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


    extension CollectionType where Index == Int{

        func shuffle() -> [Generator.Element] {

            var readyToAskQuestions : [quizController] {

                var questions = Array(question)
                questions.count = questionNum

                questions.shuffleInPlace()
                return questions
            }
            convenience init() {


            }
}

在var問題行中,我收到一條錯誤消息,說

“問題”是一個未解決的標識符

我知道該功能不在quizController類中,這就是原因,但是我需要該擴展名,以便可以隨意整理測驗問題。

我應該如何處理? 另外,我將從json文件中加載測驗問題,這就是為什么那里有一個方便的init方法,但是我收到了一條錯誤消息,指出

初始化程序只能在類型內聲明。

所以這意味着它需要在一個班級之內?

謝謝您的幫助。

試試這個代替你的代碼

var question = String()
var options = [String]()
var correctAns = Int()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM