21

I have this class file for accepting card payments

import UIKit
class PaymentViewController: UIViewController , PTKViewDelegate {

    var card : STPCard
    var PaymentView : PTKView
    var button   = UIButton.buttonWithType(UIButtonType.System) as UIButton

    init(PaymentView : PTKView , button : UIButton, card : STPCard) {
        self.PaymentView = PaymentView
        self.button = button
        self.card = card
        super.init()
    }
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
}

When I build it, it works fine, but when I execute it (run it) on my actual device , I get

fatal error: init(coder:) has not been implemented.

Any ideas ?

5
  • I know because the fatalError line is doing it, but why do I have to do a required init and why is it not just happy with an init. Commented Oct 28, 2014 at 16:07
  • the actual method throws you a fatal error... I'm not sure what you really expected from a fatalError() line – you have got exactly what that line should do: a fatal error exception. you can avoid such exception by implementing the method's body. Commented Oct 28, 2014 at 16:31
  • Yes I know Holex, but I am not sure why a REQUIRED is needed and what I should put it in. Commented Oct 28, 2014 at 16:35
  • maybe, calling the super class...? like e.g. super.init(coder: aDecoder)? oh, wait! that was not a question. Commented Oct 28, 2014 at 16:37
  • the required keyword indicates you, you have to override that method in your subset of the class. Commented Oct 28, 2014 at 16:40

2 Answers 2

20

Based on the Inheritance Hierarchy you have setup. PaymentViewController will inherit 3 init methods. UIViewController provides init(nibName:bundle) as its designated initializer. UIViewController also conforms to NSCoding which is where the required init(coder:) comes from. UIViewController also inherits from NSObject which provides a basic init() method.

UIViewController inherited init methods diagram

The problem you are having stems from init(coder: being called when the ViewController is instantiated from a .xib or storyboard. This method is called to un-archive the .xib/storyboard objects.

From the Documentation:

iOS initializes the new view controller by calling its initWithCoder: method instead.

You should be calling the superclass designated initializer in your init method, which is init(nibName:bundle) Note: it is fine for both of those parameters to be nil. Also your init(coder:) override should call super.init(coder:)

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

3 Comments

required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) }
I have exactly the same issue and calling super in required init... only says that properties self.property_name ... not initialized at super.init call.
@StanRedoute you'll want to read up on the initialize rules in swift. Short story at the end of any init method you'll need to have provided default values for all your stored properties. (this can be a little tricky in swift when using storyboards/init(coder:)) You'll probably want to make them lazy var so they can be computed after init time, or use Implicitly unwrapped optionals to tell the compiler you'll set them later. Docs: docs.swift.org/swift-book/LanguageGuide/Initialization.html
11

A simple workaround will do:

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

1 Comment

I have exactly the same issue and calling super in required init... only says that properties self.property_name ... not initialized at super.init call.

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.