官术网_书友最值得收藏!

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)
主站蜘蛛池模板: 德清县| 高陵县| 集安市| 天津市| 清水河县| 临汾市| 毕节市| 岚皋县| 苍梧县| 卢氏县| 新乐市| 松滋市| 屏东县| 嘉禾县| 甘南县| 钟山县| 晋州市| 旬阳县| 南江县| 永嘉县| 灵石县| 丽江市| 化隆| 阿拉尔市| 静宁县| 三穗县| 彩票| 金寨县| 都昌县| 长阳| 江口县| 岑溪市| 西乡县| 罗田县| 潮州市| 威宁| 古交市| 南雄市| 仙游县| 当阳市| 德州市|