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

时间:2015-01-20 09:35:24

标签: arrays variables swift if-statement task

我努力学习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
    }

我确信有一个简单的菜鸟我错过了。你们有没有看到我的错误?

1 个答案:

答案 0 :(得分:0)

您需要将其声明为

var spormslaArray = [AnyObject]()

后面的括号表示您要实例化它。只使用[]将使其类型为NSArray,它是非变异的。而不是AnyObject你应该使用你想要放入的实际类型。在你的情况下,它再次是一些数组。您可以通过声明类型

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

然后使用

var spormslaArray = [MySepcialArray]()

一旦你使用它,你就会明白这一点。