第一(一個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;
}
的第一個片段,不能轉換成Objective-C的,因爲相關類型的枚舉不可在ObjC中獲得。第二個片段可以通過實現屬性的顯式設置器來轉換。 – vadian
將Objective-C轉換爲Swift比試圖從Swift到Objective C進行一對一轉換要容易得多。Swift的枚舉是獨特而強大的,我懷疑你會找到一種簡單的方法將它轉換爲Swift –