Search code examples
swiftoverridinginitializer

how to override objective-c method in swift


I am trying to override this objective-c method in a swift subclass. I keep getting compiler errors though:

From AWSMTLModel

- (instancetype)initWithDictionary:(NSDictionary *)dictionary error:(NSError **)error

Swift subclass

override func init!(dictionary dictionaryValue: [NSObject : AnyObject]!)//error: overriding declaration requires override keyword (fix it places override after func but gives same error

Solution

  • Drop the func.

    override init?(withDictionary dict: [NSObject : AnyObject])
    

    Also, I would get rid of the implicitly unwrapped optionals. If the init can fail, make it an optional. If the dictionary parameter is optional, create another convenience init that takes no parameters, but which creates a default dictionary and calls this init.