iOS Development--Error summary & common errors and warnings in development summary (32)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.