以MBProgressHUD為例子
swift中Podfile文件一般都會加上use_frameworks! 這樣就可以直接通過import MBProgressHUD來訪問MBProgressHUD中的方法了
這個時候如果我們想要給MBProgressHUD添加一些方法,一般會使用category,在創建category的時候#import "MBProgressHUD.h"
提示無法找到對應的頭文件
導致這個問題的原因是use_frameworks!會把我們導入的第三方庫轉換成framework
swift中訪問framework的方法:
import MBProgressHUD
Objective-C中訪問framework的方法:
@import MBProgressHUD;
同樣的道理,如果我們想在swift中使用AFNetWorking,然后又想給AF初始化一些參數,可以使用@import AFNetworking; 附AFNetworking中利用runtime動態acceptableContentTypes的方法
@implementation AFJSONResponseSerializer (Serializer)
+(void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Method ovr_initMethod = class_getInstanceMethod([self class], @selector(init));
Method swz_initMethod = class_getInstanceMethod([self class], @selector(swizzlingForSetSerializer_init));
method_exchangeImplementations(ovr_initMethod, swz_initMethod);
});
}
- (id) swizzlingForSetSerializer_init {
id swz_self = [self swizzlingForSetSerializer_init];
if (swz_self && [swz_self isKindOfClass:[AFJSONResponseSerializer class]]) {
//start tiny
NSSet * contentSet = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/plain", @"text/html", nil];
[swz_self setValue:contentSet forKey:@"acceptableContentTypes"];
}
else {
NSLog(@"AFJSONResponseSerializer+Serializer kvc get AFJSONResponseSerializer error");
}
//
return swz_self;
}