- 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)
推薦閱讀
- 數據可視化:從小白到數據工程師的成長之路
- 卷積神經網絡的Python實現
- Spark大數據分析實戰
- MATLAB Graphics and Data Visualization Cookbook
- 編寫有效用例
- Power BI智能數據分析與可視化從入門到精通
- 智慧的云計算
- Web Services Testing with soapUI
- 大數據計算系統原理、技術與應用
- Machine Learning for Mobile
- 數據分析方法及應用:基于SPSS和EXCEL環境
- 數據庫原理及應用實驗:基于GaussDB的實現方法
- 數據庫高效優化:架構、規范與SQL技巧
- 工業大數據工程:系統、方法與實踐
- UnrealScript Game Programming Cookbook