4

I need to extend a class for NSCoding protocol conformance. This is what I tried:

extension GTLTasksTask : NSCoding {

    public func encodeWithCoder(aCoder: NSCoder) {

    }

    public convenience init(coder aDecoder: NSCoder) {

    }

}

But I get two errors: 1. Initializer requirement 'init(coder:)' can only be satisfied by a required initializer in the definition of non-final class 'GTLTasksTask' 2. Convenience initializer for 'GTLTasksTask' must delegate (with 'self.init')

SomeClass in this example does not have a designated initialiser, though it's super class has an init method. But as per swift documentation convenience initialisers cannot invoke super.init. I tried making init(coder) a designated initialiser, but that isn't allowed in an extension

Is it not possible to conform this to NSCoding via an extension?

codester
  • 36,891
  • 10
  • 74
  • 72
runios
  • 384
  • 1
  • 2
  • 14
  • @dcmaxx seems to have a similar issue here: http://stackoverflow.com/questions/25631727/adding-nscoding-as-an-extension – runios Oct 25 '14 at 15:03

1 Answers1

-4

Add the Require keyword. Tested with xCode 6.0 (tested in playground)

extension GLTasksTask : NSCoding {

    public func encodeWithCoder(aCoder: NSCoder) {

    }

    public required convenience init(coder aDecoder: NSCoder) {

    }
}
Konrad77
  • 2,515
  • 1
  • 19
  • 36
  • 1
    It does not work, the error says the required initialiser cannot be declared in the extension. I ran the following code in the playground and the same error: class first : NSObject { var name = "string" } class second : first { var age:Int override init() { age = 21 } } extension second : NSCoding { func encodeWithCoder(aCoder: NSCoder) { } required convenience init(coder aDecoder: NSCoder) { self.init() } } – runios Oct 25 '14 at 14:59