I am rewriting an Objective-C class in Swift to get a feel for the language. In Objective-C my class included the following method:
- (id) initWithCoder:(NSCoder *)aDecoder
{
return [self initWithActionName:[aDecoder decodeObjectOfClass:[NSString class] forKey:@"actionName"]
payload:[aDecoder decodeObjectOfClass:[NSDictionary class] forKey:@"payload"]
timestamp:[aDecoder decodeObjectOfClass:[NSDate class] forKey:@"timestamp"]];
}
After some trial and error with the compiler, I managed to rewrite it like this:
convenience init(aDecoder: NSCoder) {
let actionName = aDecoder.decodeObjectOfClass(NSString.self, forKey: "actionName") as NSString
let payload = aDecoder.decodeObjectOfClass(NSDictionary.self, forKey: "payload") as NSDictionary
let timestamp = aDecoder.decodeObjectOfClass(NSDate.self, forKey: "timestamp") as NSDate
self.init(actionName: actionName, payload: payload, timestamp: timestamp)
}
However, this still gives me an error on the last line: “Could not find an overload for init
that accepts the supplied arguments.” The method signature of init
is
init(actionName: String, payload: Dictionary<String, NSObject>, timestamp: NSDate)
so I assume the problem is that I am trying to pass an NSDictionary
instead of a Dictionary<String, NSObject>
. How can I convert between these two not-quite-equivalent types? Should I just be using NSDictionary
for all of the code that has to interact with other Objective-C components?