Swift default value for Objective-C selector parameter

848 views Asked by At

I have the following Objective-C method:

-(void)setPaneState:(MHPaneState)state withInitialVelocity:(CGPoint)velocity{
    self.paneState = state;
    [self animatePaneWithInitialVelocity:velocity];
}

Currently, in Swift code in the same app, I'm calling this method like this:

setPaneState(.closed, withInitialVelocity: .zero)

I'd like for the 2nd parameter (velocity) to have a default value of CGPointZero. I know that Objective-C doesn't support default values at the language level, but Swift does, so I'd like to call this method in Swift like this:

setPaneState(.closed)

How can I accomplish this short of defining a new method that takes just the one MHPaneState parameter?

1

There are 1 answers

0
jefflovejapan On

Could you write a Swift extension on your Objective-C type and give it a different name?

extension MyObjCThing {
    func mySetPaneState(_ state: MHPaneState, withInitialVelocity velocity: CGPoint = .zero) {
        setPaneState(state, withInitialVelocity: velocity)
    }
}