导航

Objective的宏到swift中该怎么办?

ReadMe

Q1: typedef NS_ENUM

OC中的枚举被转换成了swif中的枚举:

 
 
typedef NS_ENUM(NSInteger, PersonSexType) {
    PersonSexTypeMale,
    PersonSexTypeFemale,
    PersonSexTypeNeuter
};
 
 
public enum PersonSexType : Int { 
    case Male
    case Female
    case Neuter
}
public class Person : NSObject {
    public var sexType: PersonSexType
}

 screenshot_testMacro_2016_03_04_23_48_48

Q2: 复杂的宏改何去何从?

Complex Macros

Complex macros are used in C and Objective-C but have no counterpart in Swift.(不兼容) Complex macros are macros that do not define constants, including parenthesized, function-like macros. You use complex macros in C and Objective-C to avoid type-checking constraints or to avoid retyping large amounts of boilerplate code. However, macros can make debugging and refactoring difficult. In Swift, you can use functions and generics to achieve the same results without any compromises.(替代方法就是使用函数和泛型手段实现这些宏的功能) Therefore, the complex macros that are in C and Objective-C source files are not made available to your Swift code.

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html#//apple_ref/doc/uid/TP40014216-CH8-XID_20

A1: 使用函数替换宏

在OC中:

 
 
#define SQUARE_NUMBER(n) n * n      

 在swift中

 
 
func squareNumber(n: Int) -> Int {
    return n * n
}

A2: 稍微多几个参数的宏定义

在OC中:

 
 
#define SQUARE_NUMBER(n) n * n
#define UIColorFromRGB(rgbValue, alphaValue) \
[UIColor colorWithRed:((float)((rgbValue >> 16) & 0xFF))/255.0 \
green:((float)((rgbValue >> 8) & 0xFF))/255.0 \
blue:((float)((rgbValue >> 0) & 0xFF))/255.0 \
alpha:alphaValue]
@interface Person : NSObject
-(void)printColor;
@end
@implementation Person
-(void)printColor{
    NSLog(@"%@",UIColorFromRGB(0xffeedd,0.5));
}
@end    

然后呢,在swif只是可以使用printColor这个方法:

 
 
 let p =  Person()
  p.printColor()
  /*
  testMacro[5226:193263] UIDeviceRGBColorSpace 1 0.933333 0.866667 0.5
  */

但是并不能直接使用UIColorFromRGB(0xffeedd,0.5)这样.

解决方法,写成一个函数咯:

 
 
func UIColorFromRGB(rgb: Int, alpha: Float) -> UIColor {
    let red = CGFloat(Float(((rgb>>16) & 0xFF)) / 255.0)
    let green = CGFloat(Float(((rgb>>8) & 0xFF)) / 255.0)
    let blue = CGFloat(Float(((rgb>>0) & 0xFF)) / 255.0)
    let alpha = CGFloat(alpha)
    return UIColor(red: red, green: green, blue: blue, 
    alpha: alpha)
}
func testMacroReplacement2(){
    print(UIColorFromRGB(0xffddee, alpha: 0.5))
}
testMacroReplacement2() 

Q3: 结构体转换

在OC中会有这样的情况:

 
 
#define AD_SIZE CGSizeMake(320, 50)
@interface ADView : UIView
@property (nonatomic, assign) CGSize adSize;
-(void)printADSize;
@end
@implementation ADView
-(void)printADSize{
    NSLog(@"%@",NSStringFromCGSize(AD_SIZE));
}
@end    

在swift中呢,我们也是可以直接调用printADSize方法的:

 
 
 func testSizeStruct(){
     let adView = ADView()
     adView.printADSize()
 }
/*
 2016-03-05 00:28:26.984 testMacro[5759:213046] {320, 50}
*/

如果我们想要在OC和swift中都能用,那么可以:

在.h和.m中这样实现:

//  ADView.h
extern CGSize const MOPUB_BANNER_SIZE;

//  ADView.m
#import "ADView.h"
CGSize const MOPUB_BANNER_SIZE = { .width = 320.0f, .height = 50.0f };

在swift中可以这样使用:

 
 
 func testSizeStructExternConst(){
     let adView = ADView()
     adView.printADSize()
     print(MOPUB_BANNER_SIZE)
 }
 /*
 2016-03-05 00:36:13.002 testMacro[5924:223091] {320, 50}
(320.0, 50.0)
 */

screenshot_testMacro_2016_03_05_00_40_56

Tip:从OC头文件显示转换成Swift接口文件

  • 打开.h头文件

  • 显示辅助编辑器(alt+appleKey+enter) (View>Assistant Editor>Show Assistant Editor)

  • 选择同样的.h头文件

  • 在辅助编辑器左上角选择Generated Interface

  • Done

    show-generated-interface

参考资料:

posted on 2016-03-05 00:54  淅沥枫  阅读(2549)  评论(1编辑  收藏  举报