将变量添加到在 swift 中的 if else 之前声明的数组

Add variables to array declared before if else in swift

我正在努力学习 ios 开发,并已按照指南构建了一个简单的测验应用程序。我正在尝试通过从 json 解析将应用程序连接到数据库。 关于那部分,一切都很好。我现在想将从 json 创建的变量添加到数组中。 我在 let task:

之前声明了数组
   var spormslaArray = []

我想在任务里面的数组中添加问题,像这样:

   var question[] = [id, questionItself, answer1, answer2, answer3, answer4, correctAnswerJson]

spormslaArray.append(question)

我遇到了这些错误..

 App/ViewController.swift:127:41: Consecutive statements on a line must be separated by ';'
App/ViewController.swift:129:29: Implicit use of 'self' in closure; use 'self.' to make capture semantics explicit
The task:

任务:

let task = session.dataTaskWithURL(url, completionHandler: { (data, response, error) -> Void in
        if error != nil {
            // If there is an error in the web request, print it to the console
            println(error.localizedDescription)
        }
        else {
            var err: NSError?
            var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary
            if err != nil {
                // If there is an error parsing JSON, print it to the console
                println("JSON Error \(err!.localizedDescription)")
            }
            else {
                let questions=jsonResult["data"] as? [[String:String]]
                if (questions != nil) {
                    for question in questions! {

                        questionNummer += 1
                        let answer1=question["answerOne"]!
                        let answer2=question["answerTwo"]!
                        let answer3=question["answerThree"]!
                        let answer4=question["answerFour"]!
                        let id=question["id"]!
                        let questionItself=question["questionTemplate"]!
                        let correctAnswerJson=question["correctAnswer"]!


                        println(id, questionItself, answer1, answer2, answer3, answer4, correctAnswerJson)

   var question[] = [id, questionItself, answer1, answer2, answer3, answer4, correctAnswerJson]


               spormslaArray.append(question)





                    }
                }

            }
        }
    })

    task.resume()

问题是这样硬编码的:

let questionSeven = questionTemplate("lol", answerOne: "av ormer (worms)", answerTwo: "virus infeksjon", answerThree: "installeres av en Trojaner eller en 'datasnik'", answerFour: "Bot aktivitet", correctAnswer: 3)

然后加入数组

   spormslaArray = [questionOne, questionTwo, questionThree, questionFour, questionFive, questionSix,questionSeven]

然后发给函数设置题:

     func questionTemplate(question:String, answerOne:String, answerTwo:String, answerThree:String, answerFour:String, correctAnswer:Int) -> NSArray {

        //Set the question
        var quizQuestion = question

        //set the answers and the right answer

        var firstAnswer = answerOne
        var secondAnswer = answerTwo
        var thirdAnswer = answerThree
        var fourthAnswer = answerFour
        var rightAnswer = correctAnswer



        var gjennverendeSporsmal = 1



        //Add all the questions and answers to an array
        let questionAnswerArray = [question, firstAnswer, secondAnswer, thirdAnswer, fourthAnswer, rightAnswer]
        return questionAnswerArray
    }

我确定我缺少一个简单的菜鸟东西。你们有人看到我的错误吗?

您需要声明为

var spormslaArray = [AnyObject]()

后面的括号表示您要实例化它。仅使用 [] 将使它成为非变异的 NSArray 类型。而不是 AnyObject 你应该使用你想要放入的实际类型。在你的情况下它又是一些数组。您可以通过声明该类型来做到这一点

typealias MySpecialArray = [AnyObject] // or whatever is inside, maybe String

然后使用那个

var spormslaArray = [MySepcialArray]()

一用就懂。