Reputation: 803
Either I'm seeing some strange behaviour from Swift or I'm doing something wrong.
Let's say you have an obj-c class called TurtleHelper
which looks like this:
@interface TurtleHelper : NSObject
+(NSDictionary*)getTurtles;
@end
Then I want to override this method in Swift, so I do this:
class SwiftTurtles: TurtleHelper {
override class func getTurtles() -> NSDictionary {
// ...
}
}
The compiler throws the following error at me:
Overriding method with selector getTurtles has incompatible type
() -> NSDictionary
What am I doing wrong?
Upvotes: 5
Views: 2171
Reputation: 1562
I had a very similar issue. Using XCode 12.2 I tried to override a function quite alike:
@interface AuthService : NSObject
- (NSDictionary *)authHeaders;
@end
I tried to subclass it as in the above answer and a lot of optional, implictly unwrapped, bridged classes combinations, but what ended up working for me (Swift 4.2, XCode 12.2) was
class AuthServiceMock: AuthService {
override func authHeaders() -> [AnyHashable: Any]?
}
Upvotes: 0
Reputation: 803
It turns out that the answer seems to be
override class func getTurtles() -> [NSObject : AnyObject]!
From Apple's documentation:
When you bridge from an NSDictionary object to a Swift dictionary, the resulting dictionary is of type [NSObject: AnyObject]. You can bridge any NSDictionary object to a Swift dictionary because all Objective-C objects are AnyObject compatible.
Upvotes: 8