如何将此Swift语法转换为Objective C?

时间:2017-01-20 05:31:11

标签: objective-c swift

我正在将一个快速项目转换为目标c,但我遇到了一些麻烦,因为我不知道如何转换代码。请帮我。谢谢!

   public enum UPCarouselFlowLayoutSpacingMode {
    case fixed(spacing: CGFloat)
    case overlap(visibleOffset: CGFloat)
}

fileprivate var currentPage: Int = 0 {
    didSet {
        let character = self.items[self.currentPage]
        self.infoLabel.text = character.name.uppercased()
        self.detailLabel.text = character.movie.uppercased()
    }
}

1 个答案:

答案 0 :(得分:5)

第一个(带有关联值的enum)在Objective-C中没有直接等价物。对于您的特定示例,您可以使用以下内容:

typedef NS_ENUM(NSInteger, UPCarouselFlowLayoutSpacingMode) {
    UPCarouselFlowLayoutSpacingModeFixed,
    UPCarouselFlowLayoutSpacingModeOverlap
};

typedef struct {
    UPCarouselFlowLayoutSpacingMode mode;
    CGFloat amount;
} UPCarouselFlowLayoutSpacing;

您只需传递UPCarouselFlowLayoutSpacing类型的值。您可以创建辅助函数以使这些更容易创建,例如

UPCarouselFlowLayoutSpacing UPCarouselFlowLayoutSpacingMakeFixed(CGFloat spacing) {
    UPCarouselFlowLayoutSpacing value;
    value.mode = UPCarouselFlowLayoutSpacingModeFixed;
    value.amount = spacing;
    return value;
}

UPCarouselFlowLayoutSpacing UPCarouselFlowLayoutSpacingMakeOverlap(CGFloat visibleOffset) {
    UPCarouselFlowLayoutSpacing value;
    value.mode = UPCarouselFlowLayoutSpacingModeOverlap;
    value.amount = visibleOffset;
    return value;
}

对于第二个,您可以覆盖Objective-C类的currentPage属性的setter方法:

- (void)setCurrentPage:(NSInteger)page {
    _page = page;

    MovieCharacter *character = self.items[page];
    self.infoLabel.text = character.name.localizedUppercaseString;
    self.detailLabel.text = character.movie.localizedUppercaseString;
}