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.

Community
  • 1
  • 1
  • You need to flag your subclass `init` method as failable by including the ? As the superclass initialiser is failable. – Paulw11 Jun 07 '16 at 06:52
  • @Paulw11 can you please elaborate by posting some code. I am very new to swift . – Sushree Swagatika Jun 07 '16 at 07: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!! – Sushree Swagatika Jun 07 '16 at 07: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? – Paulw11 Jun 07 '16 at 07:16
  • @Paulw11 I have posted my full code. Could you please help me in finding out the solution ? – Sushree Swagatika Jun 07 '16 at 07:23
  • How do you create the custom view; where do you call `init(coder)` ? Have you tried setting a breakpoint in your init method and single stepping through to find the problem? – Paulw11 Jun 07 '16 at 07:25

2 Answers2

2

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.

Community
  • 1
  • 1
1

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

Mesut GUNES
  • 5,532
  • 2
  • 22
  • 43