gpt4 book ai didi

swift - 初始化类对象 swift 3

转载 作者:行者123 更新时间:2023-11-28 10:14:44 25 4
gpt4 key购买 nike

我在初始化某个类的对象时遇到问题。这有什么问题? (我可以上传我所有的代码,但如果需要的话它会很大)enter image description here

编辑:我的 View Controller 代码:

    import UIKit

class ViewController: UIViewController{

@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var answerStackView: UIStackView!

// Feedback screen
@IBOutlet weak var resultView: UIView!
@IBOutlet weak var dimView: UIView!
@IBOutlet weak var resultLabel: UILabel!
@IBOutlet weak var feedbackLabel: UILabel!
@IBOutlet weak var resultButton: UIButton!

@IBOutlet weak var resultViewBottomConstraint: NSLayoutConstraint!
@IBOutlet weak var resultViewTopConstraint: NSLayoutConstraint!

var currentQuestion:Question?

let model = QuizModel()
var questions = [Question]()

var numberCorrect = 0

override func viewDidLoad() {
super.viewDidLoad()
model.getQuestions()
}

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nil, bundle: nil)
}

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


func setAll(questionsReturned:[Question]) {
/*
// Do any additional setup after loading the view, typically from a nib.

// Hide feedback screen
dimView.alpha = 0

// Call get questions
questions = questionsReturned

// Check if there are questions
if questions.count > 0 {

currentQuestion = questions[0]

// Load state
loadState()

// Display the current question
displayCurrentQuestion()
}
*/
print("Called!")

}


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

我的 QuizModel 代码:

 import UIKit
import FirebaseDatabase


class QuizModel: NSObject {

override init() {
super.init()
}

var ref:FIRDatabaseReference?
var test = [[String:Any]]()
var questions = [Question]()
weak var prot:UIPageViewControllerDelegate?
var first = ViewController()

func getQuestions(){
getRemoteJsonFile()
}


func pars(){


/*let array = test
var questions = [Question]()

// Parse dictionaries into Question objects
for dict in array {

// Create question object
let q = Question()

// Assign question properties
q.questionText = dict["question"] as! String
q.answers = dict["answers"] as! [String]
q.correctAnswerIndex = dict["correctIndex"] as! Int
q.module = dict["module"] as! Int
q.lesson = dict["lesson"] as! Int
q.feedback = dict["feedback"] as! String

// Add the question object into the array
questions += [q]
}
*/

//Protocol setAll function
first.setAll(questionsReturned: questions)
}

func getRemoteJsonFile(){

ref = FIRDatabase.database().reference()

ref?.child("Quiz").observeSingleEvent(of: .value, with: { (snapchot) in

print("hey")
let value = snapchot.value as? [[String:Any]]
if let dict = value {
self.test = dict
self.pars()
}
})
}

这不是我的全部代码,但我认为这是最重要的部分。在 QuizModel 代码中,我将我的代码从获取 json 文件重新设置为从 firebase 获取数据,这样你就可以看到像“getRemoteJSONFile”这样的函数名称,并且在解析函数中解析 json,但这不是我的问题,我认为

最佳答案

看起来您在初始化常量所在的 viewController 之前尝试初始化常量。您必须将其设为 var 并在 viewDidLoad 中对其进行初始化>(或其他生命周期方法),或将其设为 lazy var 并在首次访问时对其进行初始化。

关于swift - 初始化类对象 swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42703564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com