Summary of common errors and warnings in development (32)
Miss Miss Swift version of the classic error
Init method key points of knowledge
Problem Description:
fatal error: use of unimplemented initialer for class
As follows:
Code two:
1 Import UIKit2 classviewcontroller:uiviewcontroller{3var data:string?4 5 Init (para:string) {6 //so the parameters are initialized here. Self.data = para7 Super.init ()8 //or let's initialize it here? self.data = para9 }Ten Required Init (coder Adecoder:nscoder) { OneFatalError ("Init (coder:) has not been implemented") A } - Overridefunc viewdidload () { - super.viewdidload () the } -}
Although you have a init(para:String) constructor, there is one requried init() , but it is still unavoidable to run fatal error: use of unimplemented initialer init(nibName nibNameorNil:String?,bundle,nibBundleOrNil:NSBundle?) when an error like this
Workaround:
Workaround:
To add a function:
Init (nibname nibnameornil:string?,bundle,nibbundleornil:nsbundle?) { super.init (nibname:nibnameornil,bundle:nibbundleornil)}
Not just the solution!
Here is a question, which is also mentioned in the previous code comment, where is the initialization parameter placed? You can try to let vc = ViewController(para:"Hello") initialize a viewcontroller, you will find self.data = para where the results are not the same, placed in the Super.init () before the initialization will eventually become nil , the latter will not affect.
Init () has an ordinance that initializes the attribute values for this instance, plus super.init() , if you want to modify the parameters of the parent class, then override the assignment later! But it seems to be completely different from ours. In fact, you have overlooked a problem:
Note that we have called super.init() instead ofsuper.init(nibName:nil,bundle:nil)
Yes, the key to the problem is here. And why is there no problem after Super.init ()? Because the let vc = ViewController(para:"Hello") init(para:String) method is skipped first, the execution super.init() will jump to the init(nibName nibNameorNil:String?,bundle,nibBundleOrNil:NSBundle?) method. After performing the jump back. It's a very interesting thing that we can look into.
iOS Development--Error summary & common errors and warnings in development summary (32)