1

Just a newbie to the ios programming, and decided to start coding ios application using Xcode 6.1 swift language.

I created a new project. Then added a new Cocoa class DrawView. Then clicked main.storyboard main view and in the identity inspector put int he field class - DrawView.

Then I chose DrawView.swift - uncomment drawRect method and put into it

var context:CGContextRef = UIGraphicsGetCurrentContext()
CGContextClearRect(context, self.bounds)

and started a project, the problem is

Path/DrawView.swift: 11: 11: fatal error: use of unimplemented initializer 'init(coder:)' for class '_d_Quartz1.DrawView'

What shoud I do in oder to avoid this problem?

The entire code:

class DrawView: UIView {

    init(frame: CGRect) {
        super.init(frame: frame)
        // Initialization code
    }
    override func drawRect(rect: CGRect)
    {
       var context = UIGraphicsGetCurrentContext()
        CGContextClearRect(context, self.bounds)
    }
}
Olexiy Pyvovarov
  • 870
  • 2
  • 17
  • 32

1 Answers1

-2

You'r missing this:

required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

Just add this to your class, it should work.

Update: Follow this link to get the detailed answer when to override this method: Class does not implement its superclass's required members

Community
  • 1
  • 1
puru020
  • 808
  • 7
  • 20
  • This answer would be better if you could also explain _why_ it is needed. What the purpose of `initWithCoder` is compared to `initWithFrame`, etc. – David Rönnqvist Sep 10 '14 at 20:11
  • Here's the explanation: http://stackoverflow.com/questions/25126295/swift-class-does-not-implement-its-superclasss-required-members – puru020 Sep 10 '14 at 20:15
  • 2
    The real problem with this answer is that it won't fix the original problem, because Olexiy is loading his DrawView from a storyboard, which requires `init(coder:)` to be implemented properly, not stubbed with `fatalError`. However, it's on the right track. I'll remove my downvote if it's fixed. – rob mayoff Sep 10 '14 at 20:19
  • Well, now in my log i have
    fatal error: init(coder:) has not been implemented: file /Users/alex/Documents/Xcode/Projects/2d_Quartz1/DrawView.swift, line 19
    – Olexiy Pyvovarov Sep 10 '14 at 20:29
  • if type something like this - the project starts normally init(coder aDecoder: NSCoder!) { super.init(coder: aDecoder) } – Olexiy Pyvovarov Sep 10 '14 at 20:42