0

我正在将此Objective-C 开源项目转换为 Swift。它有一堆可选的代表。

@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;

我像这样将它们转换为 Swift。

@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)
}

在 Objective-C 项目中,在调用这些可选方法之前,它会respondsToSelector像这样检查它的可用性。

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; 

我通过使BEMAnalogClockDelegate符合NSObjectProtocol并像这样调用它们直接将其转换为 Swift。

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
}

但这会在项目运行时导致 EXC_BREAKPOINT 崩溃。

我在谷歌上搜索了一个解决方案,并看到了这篇推荐使用guard let的文章。

但是我的问题是如何指定else零件中的逻辑?例如,在上面的第一个if else块中,它检查if块中的方法,如果失败,它将值设置为块中的graduationColorto 。UIColor.whiteColor()else

如果我使用这种方法,我该怎么做guard let?还是有更好的方法来做到这一点?

4

1 回答 1

2

Swift 也支持方法的可选项。

你可以这样做:

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

或者很长的路:

if let graduationColor = delegate.analogClock?(self, graduationColorForIndex: i) {
   self.graduationColor = graduationColor
} else  {
   self.graduationColor = UIColor.whiteColor()
}
于 2016-04-07T14:18:36.473 回答