我的本地JSON序列化/解析不起作用?

时间:2016-01-05 18:41:45

标签: ios json swift local

视频在运行时显示问题:https://www.youtube.com/watch?v=INVTDEJ8ZqY

我在从本地JSON文件中正确解析JSON,将其存储到数组中然后在我的视图中显示它时遇到了很多麻烦。

我的应用没有崩溃。我的4个按钮和1个标签根本不会改变值。他们只是得到我在那里添加的那些样本值"问题1","问题2"等...

如何让它显示我的JSON文件中的值?它看起来像this

/* Question label storyboard outlet */
@IBOutlet weak var questionLabel: UILabel!

// Answer button storyboard outlets
@IBOutlet weak var topLeftButton: UIButton!
@IBOutlet weak var topRightButton: UIButton!
@IBOutlet weak var bottomLeftButton: UIButton!
@IBOutlet weak var bottomRightButton: UIButton!

/* Questions Set */
var questionObjects = NSMutableOrderedSet(capacity: 100)

override func viewDidLoad() {
    super.viewDidLoad()

    getQuestionsFromJSONFile()
    displayQuestion(0, jsonObjectsSet: questionObjects)

    let string = "some Random Question"
    questionLabel.text = string.capitalizeFirstCharacter
}

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

func getQuestionsFromJSONFile() {
    var questionsJSONData: NSData? = nil
    do {
        let pathToQuestionsJSONFile = try NSBundle.mainBundle().pathForResource("Trivia_100-199", ofType: "json")
        do {
            questionsJSONData = try NSData(contentsOfFile: pathToQuestionsJSONFile!, options: NSDataReadingOptions.DataReadingMappedIfSafe)
            let questionsJSONContent = JSON(data: questionsJSONData!)
            if let object : [String:[String:AnyObject]] = questionsJSONContent.object as? [String:[String:AnyObject]] {
                parseJSONContentIntoObjects(object)
            }

        } catch let error as NSError {
            print(error.localizedDescription)
        }
        /*
        if let questionsJSONContent = JSON(data: questionsJSONData!) as? [String: [String : AnyObject]] {
            parseJSONContentIntoObjects(questionsJSONContent) // PSE: Potential Source of Error
        }
        */
    } catch let error as NSError {
        print(error.localizedDescription)
        print(error.localizedFailureReason)
        print(error.localizedRecoveryOptions)
        print(error.localizedRecoverySuggestion)
    }
}

func parseJSONContentIntoObjects(jsonData: [String: [String : AnyObject]]) {
    var questionHasAProperAnswer = false // indicator to determine if a proper answer was provided by the API for this specific question
    let jsonObjects = NSMutableOrderedSet(capacity: 100)
    for (questionAnswer,questionPropertiesSubJson):(String,[String:AnyObject]) in jsonData {
        if let questionObject = Question(questionObjectSubJson: questionPropertiesSubJson) as? Question { // All parsing code is in the model file (Question.swift)
        if let answerText = questionObject.questionAnswer as? String {
            let questionExclusionKeywords = ["the Earth probably", "boring", "uninteresting", "unremarkable", "we do not know", "nothing", "remarkable", "a number for which", "missing a fact"]
            for questionExclusionKeyword in (questionExclusionKeywords as? [String])! {
                questionHasAProperAnswer = !answerText.containsString(questionExclusionKeyword)
             /*
                if questionHasAProperAnswer {
                    jsonObjects.addObject(questionObject)
                }
            }

            if answerText.containsAny(questionExclusionKeywords) {
                jsonObjects.addObject(questionObject)
                }
            */
            jsonObjects.addObject(questionObject)
            }
        }
    }
    questionObjects = jsonObjects
    if questionObjects.count == 0 {
        let fillerObject = Question(questionText: "Some question", questionAnswer: 1234, questionFalseAnswer1: "333", questionFalseAnswer2: "444", questionFalseAnswer3: "555")
        questionObjects.addObject(fillerObject)
    }

}
}

func displayQuestion(questionIndex: Int, jsonObjectsSet: NSMutableOrderedSet) {
    if let specificQuestionObject = jsonObjectsSet.objectAtIndex(questionIndex) as? Question {
        if let questionText = specificQuestionObject.questionText {
            questionLabel.text = questionText
        }
        if let questionAnswer = specificQuestionObject.questionAnswer as? String {
            let capitalizedQuestionAnswer = questionAnswer.capitalizeFirstCharacter
            topLeftButton.titleLabel?.text = capitalizedQuestionAnswer
        }
        bottomLeftButton.titleLabel?.text = "Some false answer"
        topRightButton.titleLabel?.text = "Some other false answer"
        bottomRightButton.titleLabel?.text = "Another random false answer"
    }
}

1 个答案:

答案 0 :(得分:2)

对于按钮,您应该使用setTitle(forState:)。例如:

button.setTitle("foo", forState: .Normal)