12
votes

When I create a subclass of UIView or UIViewController with a stored property, Xcode will not compile my project unless I include an implementation of required init?(coder aDecoder: NSCoder). Currently, I have the following implementation to shut the compiler up:

required init?(coder aDecoder: NSCoder) {
    fatalError()
}

I understand why I'm required to include this initializer; my subclass needs to conform to the NSCoding protocol because its superclass conforms to it, and this initializer is part of the NSCoding protocol so it needs to work with my class, i.e. initialize all of my class's stored properties (which the superclass version of the initializer won't do).

I imagine that a correct implementation would look something like this:

class MyView: UIView {
    let label: UILabel

    override init(frame: CGRect) {
        label = UILabel()
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        if let label = aDecoder.decodeObject() as? UILabel {
            self.label = label
        } else {
            return nil
        }
        super.init(coder: aDecoder)
    }

    override func encode(with aCoder: NSCoder) {
        aCoder.encode(label)
        super.encode(with: aCoder)
    }
}

However, considering that my application has over 50 custom views and view controllers, correctly implementing this function in every custom view and view controller is a lot of work.

So, I'm wondering if it's necessary to implement this initializer correctly, or if I can just leave it throwing a fatal error. In other words, will this initializer ever be called if I don't call it in my own code? I think I read that it might be called by a Storyboard, but my app doesn't use any Storyboards.

1
This initialiser will be called if an instance of your view is used in a storyboard scene or xib file.Paulw11
@Paulw11, you should post that as the answer.Duncan C

1 Answers

8
votes

This initialiser will be called if an instance of your view is used in a storyboard scene.

It is up to you whether to create a functioning initialiser or not, but it should mostly be a matter of copying code from init(frame:)