swiftlint使用參考
http://www.lxweimin.com/p/aaadbe445a3a
http://www.lxweimin.com/p/a1afc52ec0ff
iOS替換AppIcon 參考
Swift開發替換方法
1、OC開發的時候再+load中替換方法。swift開發沒有load方法了,替換方法實現方式有兩種
1.1使用OC
創建OC拓展文件, 將文件引入到混合開發橋接文件中
// 去掉切換AppIcon的彈框, 這里用的別人的代碼。 文件名稱是 UIViewController+YJPresent_NoAlert.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIViewController (XZPresent_NoAlert)
@end
NS_ASSUME_NONNULL_END
// UIViewController+XZPresent_NoAlert.m
#import <objc/runtime.h>
@implementation UIViewController (XZPresent_NoAlert)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Method presentM = class_getInstanceMethod(self.class, @selector(presentViewController:animated:completion:));
Method presentSwizzlingM = class_getInstanceMethod(self.class, @selector(xz_presentViewController:animated:completion:));
method_exchangeImplementations(presentM, presentSwizzlingM);
});
}
- (void)xz_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
if ([viewControllerToPresent isKindOfClass:[UIAlertController class]]) {
UIAlertController *alertController = (UIAlertController *)viewControllerToPresent;
if (alertController.title == nil && alertController.message == nil) {
return;
}
}
[self xz_presentViewController:viewControllerToPresent animated:flag completion:completion];
}
@end
在橋接文件中引入
#import "UIViewController+YJPresent_NoAlert.h"
1.2使用swift方式替換方法
創建分類添加公共類方法實現替換
例1:
extension PhotoBrowserController {
public class func initializeMethod(){
let originalSelector = #selector(PhotoBrowserController.updateNavigation)
let swizzledSelector = #selector(PhotoBrowserController.switchUpdateNavigation)
let originalMethod = class_getInstanceMethod(self, originalSelector)
let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)
//在進行 Swizzling 的時候,需要用 class_addMethod 先進行判斷一下原有類中是否有要替換方法的實現
let didAddMethod: Bool = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod!), method_getTypeEncoding(swizzledMethod!))
//如果 class_addMethod 返回 yes,說明當前類中沒有要替換方法的實現,所以需要在父類中查找,這時候就用到 method_getImplemetation 去獲取 class_getInstanceMethod 里面的方法實現,然后再進行 class_replaceMethod 來實現 Swizzing
if didAddMethod {
class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod!), method_getTypeEncoding(originalMethod!))
} else {
method_exchangeImplementations(originalMethod!, swizzledMethod!)
}
}
@objc func switchUpdateNavigation() {
switchUpdateNavigation()
if let t = super.title {
let arr = t.split(separator: " ")
self.pbNumLb.text = String(arr[0])+"/"+String(arr[2])
}
}
}
例2:
// 替換AppIcon禁止彈框
extension UIViewController {
public class func initializeMethod2(){
let originalSelector = #selector(UIViewController.yj_present(viewControllerToPresent:animated:completion:))
let swizzledSelector = #selector(UIViewController.present(_:animated:completion:))
let originalMethod = class_getInstanceMethod(self, originalSelector)
let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)
//在進行 Swizzling 的時候,需要用 class_addMethod 先進行判斷一下原有類中是否有要替換方法的實現
let didAddMethod: Bool = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod!), method_getTypeEncoding(swizzledMethod!))
//如果 class_addMethod 返回 yes,說明當前類中沒有要替換方法的實現,所以需要在父類中查找,這時候就用到 method_getImplemetation 去獲取 class_getInstanceMethod 里面的方法實現,然后再進行 class_replaceMethod 來實現 Swizzing
if didAddMethod {
class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod!), method_getTypeEncoding(originalMethod!))
} else {
method_exchangeImplementations(originalMethod!, swizzledMethod!)
}
}
@objc func yj_present(viewControllerToPresent: UIViewController, animated: Bool, completion: (() -> Void)?) {
if let alertC = viewControllerToPresent as? UIAlertController {
if alertC.title == nil && alertC.message == nil {
return
}
}
self.yj_present(viewControllerToPresent: viewControllerToPresent, animated: animated, completion: completion)
}
}
使用:
在AppDelegate.swift文件中的 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool 方法中調用這個公開的類方法
// 替換原來的方法,以正確方式顯示圖片數量
PhotoBrowserController.initializeMethod()
UIViewController.initializeMethod2()