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

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)
主站蜘蛛池模板: 郎溪县| 汕尾市| 金堂县| 惠州市| 大名县| 英德市| 库车县| 虞城县| 南江县| 乌审旗| 始兴县| 手游| 越西县| 曲沃县| 团风县| 仁怀市| 城固县| 来安县| 高青县| 新郑市| 古蔺县| 邛崃市| 资中县| 龙南县| 江都市| 涟水县| 方正县| 金坛市| 怀来县| 札达县| 遂平县| 阜宁县| 金秀| 仁布县| 巫山县| 五原县| 贡嘎县| 塔河县| 城口县| 东丰县| 东兴市|