iosobjective-cswiftrespond-toswift2

respondsToSelector on optional delegate methods


I'm converting this Objective-C open source project to Swift. It has a bunch of optional delegates.

@protocol BEMAnalogClockDelegate <NSObject>

@optional

- (void)currentTimeOnClock:(BEMAnalogClockView *)clock Hours:(NSString *)hours Minutes:(NSString *)minutes Seconds:(NSString *)seconds;
- (NSString *)dateFormatterForClock:(BEMAnalogClockView *)clock;
- (NSString *)timeForClock:(BEMAnalogClockView *)clock;
- (UIColor *)analogClock:(BEMAnalogClockView *)clock graduationColorForIndex:(NSInteger)index;
- (CGFloat)analogClock:(BEMAnalogClockView *)clock graduationAlphaForIndex:(NSInteger)index;
- (CGFloat)analogClock:(BEMAnalogClockView *)clock graduationWidthForIndex:(NSInteger)index;
- (CGFloat)analogClock:(BEMAnalogClockView *)clock graduationLengthForIndex:(NSInteger)index;
- (CGFloat)analogClock:(BEMAnalogClockView *)clock graduationOffsetForIndex:(NSInteger)index;

I converted them to Swift like so.

@objc protocol BEMAnalogClockDelegate {
    optional func currentTimeOnClock(clock: BEMAnalogClockView, hours: String, minutes: String, seconds: String)
    optional func dateFormatterForClock(clock: BEMAnalogClockView) -> String
    optional func timeForClock(clock: BEMAnalogClockView) -> String
    optional func analogClock(clock: BEMAnalogClockView, graduationColorForIndex index: Int) -> UIColor
    optional func analogClock(clock: BEMAnalogClockView, graduationAlphaForIndex index: Int) -> CGFloat
    optional func analogClock(clock: BEMAnalogClockView, graduationWidthForIndex index: Int) -> CGFloat
    optional func analogClock(clock: BEMAnalogClockView, graduationLengthForIndex index: Int) -> CGFloat
    optional func analogClock(clock: BEMAnalogClockView, graduationOffsetForIndex index: Int) -> CGFloat
    optional func clockDidBeginLoading(clock: BEMAnalogClockView)
    optional func clockDidFinishLoading(clock: BEMAnalogClockView)
}

In the Objective-C project, before calling these optional methods, it checks its availability using respondsToSelector like this.

if ([self.delegate respondsToSelector:@selector(analogClock:graduationColorForIndex:)]) {
    self.graduationColor = [self.delegate analogClock:self graduationColorForIndex:i];
} else self.graduationColor = [UIColor whiteColor];

if ([self.delegate respondsToSelector:@selector(analogClock:graduationAlphaForIndex:)]) {
    self.graduationAlpha = [self.delegate analogClock:self graduationAlphaForIndex:i];
} else self.graduationAlpha = 1.0; 

I directly converted this to Swift by making the BEMAnalogClockDelegate conform to NSObjectProtocol and calling them like so.

if delegate.respondsToSelector(#selector(BEMAnalogClockDelegate.analogClock(_:graduationColorForIndex:))) {
    graduationColor = delegate.analogClock!(self, graduationColorForIndex: i)
} else {
    graduationColor = UIColor.whiteColor()
}

if delegate.respondsToSelector(#selector(BEMAnalogClockDelegate.analogClock(_:graduationAlphaForIndex:))) {
    graduationAlpha = delegate.analogClock!(self, graduationAlphaForIndex: i)
} else {
    graduationAlpha = 1
}

But this causes a EXC_BREAKPOINT crash when the project is run.

I googled for a solution and came across this article which recommends using guard let for this.

However my problem is how do I specify the logic that goes in the else part? For example in the first if else block above, it checks the method in the if block and if it fails, it sets the value for graduationColor to UIColor.whiteColor() in the else block.

How do I do that if I use the guard let approach? Or is there a better way to do this overall?


Solution

  • Swift supports optionals for methods as well.

    You can do this instead:

    graduationColor = delegate.analogClock?(self, graduationColorForIndex: i) ?? UIColor.whiteColor()

    Or the long way:

    if let graduationColor = delegate.analogClock?(self, graduationColorForIndex: i) {
       self.graduationColor = graduationColor
    } else  {
       self.graduationColor = UIColor.whiteColor()
    }