- Hands-On Design Patterns with Swift
- Florent Vilmart Giordano Scalzo Sergio De Simone
- 140字
- 2021-07-02 14:45:15
Setting Swift names from Objective-C
Clang also provides macros that help to rename Objective-C classes and methods for Swift: NS_SWIFT_NAME.
Let's start with an example that could arise if you're writing a cooking application or any other app that requires measuring. When cooking, you'll often be hit with multiple volumes, units, and so on. It is possible to abstract it with a Measure class:
// A measuring unit
typedef NS_ENUM(NSInteger, FVMeasureUnit) {
FVLiters,
FVMilliliters,
FVCups
};
// The Measure class,
@interface FVMeasure : NSObject
@property (nonatomic, readonly) double amount;
@property (nonatomic, readonly) FVMeasureUnit unit;
+ (instancetype) measureWithAmount:(double) amount unit:(FVMeasureUnit) unit;
+ (instancetype) withCups:(double) value;
- (double) valueInUnit:(FVMeasureUnit) unit;
/* more convenience initializers */
@end
While the previous code is not explicitly bad, it is not very Swift-y:
let measure = FVMeasure(amount: 10, unit: FVMeasureUnit.cups)
let inMilliliters = measure.value(in: .milliliters)
推薦閱讀
- 在你身邊為你設(shè)計(jì)Ⅲ:騰訊服務(wù)設(shè)計(jì)思維與實(shí)戰(zhàn)
- Microsoft SQL Server企業(yè)級(jí)平臺(tái)管理實(shí)踐
- SQL Server 2016 數(shù)據(jù)庫教程(第4版)
- 信息系統(tǒng)與數(shù)據(jù)科學(xué)
- 數(shù)據(jù)之巔:數(shù)據(jù)的本質(zhì)與未來
- R數(shù)據(jù)科學(xué)實(shí)戰(zhàn):工具詳解與案例分析(鮮讀版)
- 區(qū)塊鏈通俗讀本
- 數(shù)據(jù)驅(qū)動(dòng)設(shè)計(jì):A/B測試提升用戶體驗(yàn)
- INSTANT Cytoscape Complex Network Analysis How-to
- OracleDBA實(shí)戰(zhàn)攻略:運(yùn)維管理、診斷優(yōu)化、高可用與最佳實(shí)踐
- Hadoop 3.x大數(shù)據(jù)開發(fā)實(shí)戰(zhàn)
- 編寫有效用例
- 淘寶、天貓電商數(shù)據(jù)分析與挖掘?qū)崙?zhàn)(第2版)
- 智慧城市中的大數(shù)據(jù)分析技術(shù)
- Oracle 11g+ASP.NET數(shù)據(jù)庫系統(tǒng)開發(fā)案例教程