前言
項目中使用CocoaPod引入一個第三方庫往史,使用過程中發(fā)現(xiàn)需要調(diào)用第三方庫中的一個私有方法实胸。
可選的方案有兩個:
- 使用runtime hook第三方庫的方法
- 在使用類中使用類別暴露第三方庫的方法
但考慮到后續(xù)第三方庫有可能會更新的問題,為了降低維護(hù)成本谤职,最終選擇了方案2长捧。
代碼類似于:
//
// Person.h
// TestDemo
//
// Created by Ella on 2017/7/27.
// Copyright ? 2017年 Ella. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Person : NSObject
- (void)getMyName;
@end
//
// Person.m
// TestDemo
//
// Created by Ella on 2017/7/27.
// Copyright ? 2017年 Ella. All rights reserved.
//
#import "Person.h"
@implementation Person
- (void)__init {
NSLog(@"__init function");
}
- (void)getMyName {
NSLog(@"I'm a humanbeing and my name is Johny");
}
@end
在調(diào)用的類中定義類別:
//
// ViewController.m
// TestDemo
//
// Created by Ella on 2017/7/27.
// Copyright ? 2017年 Ella. All rights reserved.
//
#import "ViewController.h"
#import "Person.h"
@interface ViewController ()
@end
@interface Person()
- (void)__init;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Person *person = [[Person alloc] init];
[person getMyName];
[person __init];
}