我正在初始化某个类的对象时遇到问题。这有什么问题? (我可以上传我的所有代码,但如果需要,它会很大)
编辑: 我的视图控制器代码:
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的解析函数中,但这不是我的问题的问题我认为
答案 0 :(得分:2)
在您初始化它所在的viewController之前,您似乎正在尝试初始化常量。您必须将其设为var
并在viewDidLoad
(或其他生命周期方法)中初始化它),或者将其设为lazy var
并在首次访问时对其进行初始化。
答案 1 :(得分:2)
问题可归结为以下几点:
ViewController
在对象初始化期间调用变量初始值设定项。当您创建QuizModel
的实例时,会创建ViewController
的实例,但其初始化会创建QuizModel
,再次创建新的QuizModel
,依此类推。
这个无限循环迟早会使你的应用程序崩溃(所谓的“堆栈溢出”)。
你身边可能有些错误。也许你想把初始控制器传递给class ViewController ... {
lazy var model: QuizModel = {
return QuizModel(viewController: self)
}
}
class QuizModel ... {
weak var viewController: ViewController?
override init(viewController: ViewController) {
self.viewController = viewController
}
}
,例如?
weak
另外请确保您没有创建所有权周期(这就是我使用{{1}}的原因)。
一般来说,严格区分模型类和UI类是个好主意。您不应将视图控制器(或页面视图控制器委托)传递给模型类。如果您需要与控制器通信,请为此创建专用代理。