0

init(coder aDecoder:)

I can't exactly understand what have I done wrong. I tried removing the optional(?) but with no results.

Image showing error after '?' removed

Here's the total code of my Custom View :-

class CustomView: UIView {

    var vieww: UIView!

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupView()
    }

    func loadViewFromib () -> UIView {
        let bundle = NSBundle(forClass: self.dynamicType)
        let nib = UINib(nibName: NSStringFromClass(self.dynamicType).componentsSeparatedByString(".").last!, bundle: bundle)
        let view = nib.instantiateWithOwner(self, options: nil).first as! UIView
    
        return view
    }

    func setupView() {
        vieww = loadViewFromib()
    
        vieww.frame = CGRectMake(0, 0, 300, 150)
        vieww.center = center
        addSubview(vieww)
    
        /// Adds a shadow to our view
        vieww.layer.cornerRadius = 4.0
        vieww.layer.shadowColor = UIColor.blackColor().CGColor
        vieww.layer.shadowOpacity = 0.2
        vieww.layer.shadowRadius = 4.0
        vieww.layer.shadowOffset = CGSizeMake(0.0, 8.0)
    }

I am new to swift and don't know what to do exactly. Please help me out.

Thanks in advance.

6
  • You need to flag your subclass init method as failable by including the ? As the superclass initialiser is failable. Commented Jun 7, 2016 at 6:52
  • @Paulw11 can you please elaborate by posting some code. I am very new to swift . Commented Jun 7, 2016 at 7:07
  • @Paulw11 as you can see in the first image provide by me in my question I have done the same but still error shows up!! Commented Jun 7, 2016 at 7:12
  • Yes, sorry, your question is confusing. You should post code rather than images. The most common reason for that exception is something being nil. How are you creating this view object? Where is the NSCoder coming from? Commented Jun 7, 2016 at 7:16
  • @Paulw11 I have posted my full code. Could you please help me in finding out the solution ? Commented Jun 7, 2016 at 7:23

2 Answers 2

3

The error was caused due to a infinite loop and also due to the fact that I was not providing any frame to the CustomView. I found it after setting breakpoints and going step by step.

Here's the solution (code in CustomView):-

override init(frame: CGRect) {
    super.init(frame: frame)
    setupView()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    setupView()
}

func setupView() {

  // do all your setup for your view here
}

In your ViewController ->

lazy var popupView :CustomView = {
    let popupView = CustomView(frame: CGRectMake(0,0,300,150))
    return popupView
}()

Here's a link from stack overflow that cleared my all of my doubts.

Fatal error: use of unimplemented initializer 'init(coder:)' for class

If still anyone has any doubts you can refer http://www.edwardhuynh.com/blog/2015/02/16/swift-initializer-confusion/ . This blog will definitely clear all of your doubts.

Hope somebody finds these useful.

Sign up to request clarification or add additional context in comments.

Comments

2

Setting the Module in the Custom Class under Inspector solves my problem. You can set the module same with custom class or the class inherited from.

enter image description here

1 Comment

I was so darn confused for so long! This really helped!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.