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

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)
主站蜘蛛池模板: 湟源县| 潜江市| 丰原市| 祁阳县| 邓州市| 南木林县| 洪洞县| 广丰县| 萨迦县| 唐海县| 尚义县| 新巴尔虎右旗| 凌源市| 舞钢市| 社旗县| 友谊县| 冀州市| 东丽区| 靖西县| 中牟县| 澄江县| 绥阳县| 大邑县| 百色市| 淮阳县| 克东县| 修水县| 白山市| 镇宁| 彩票| 绵阳市| 四平市| 余庆县| 绍兴市| 大新县| 吴川市| 大庆市| 扶绥县| 乐东| 绥德县| 桓台县|