服务器在给客户端接口的时候一般是json,正常情况下约定是NSString类型,但是有时候服务器给到就是NSNumber类型。如果是需要展示的数据,label.text = @(100),这样就会崩溃。所以下面有3种方式来防止这个崩溃。
第一种方式:
最推荐使用,category添加方法,然后做交换
#import <UIKit/UIKit.h>
@interface UILabel (SetText)
@end
#import "UILabel+SetText.h"
#import <objc/runtime.h>
@implementation UILabel (SetText)
+ (void)load {
SEL origSel = @selector(setText:) ;
SEL altSel = @selector(setTextHooked:) ;
Method origMethod = class_getInstanceMethod(self, origSel);
Method altMethod = class_getInstanceMethod(self, altSel);
if (!origMethod || !altMethod) {
return ;
}
// 交换实现
method_exchangeImplementations(origMethod,altMethod);
}
/// 主要是解决 服务器有时返回NSNumber类型,但是用了NSString指针接收,在 label.text = @(num) 时崩溃
- (void) setTextHooked:(NSString *)string {
if (string == nil) {
string = @"";
}
if ([string isKindOfClass:[NSNumber class]]) {
NSLog(@"怎么回事,小兄弟,传个Number过来,传统方法,推荐使用");
string = string.description;
}
if ([string isKindOfClass:[NSString class]] && [string containsString:@"(null)"]) {
string = [string stringByReplacingOccurrencesOfString:@"(null)" withString:@""];
}
[self setTextHooked:string.description] ;
}
@end
第二种方式:
不需要类别,主体思路和第一种类似,在外界给UILabel添加一个方法。看别人这么写的
#import <Foundation/Foundation.h>
@interface OhterHook2 : NSObject
@end
#import "OhterHook2.h"
#import <objc/runtime.h>
#import <objc/message.h>
#import <UIKit/UIKit.h>
@implementation OhterHook2
+ (void)load {
SEL origSel = @selector(setText:) ;
SEL altSel = @selector(setTextHooked:) ;
Method origMethod = class_getInstanceMethod([UILabel class], origSel);
// 给UILabel添加一个方法,然后在交换.不需要在类别中实现
class_addMethod([UILabel class],altSel,
(IMP)setTextHooked,
"v@:@");
Method altMethod = class_getInstanceMethod([UILabel class], altSel);
method_exchangeImplementations(origMethod,altMethod);
}
/// 主要是解决 服务器有时返回NSNumber类型,但是用了NSString指针接收,在 label.text = @(num) 时崩溃
void setTextHooked(id self , SEL _cmd,NSString * string) {
if (string == nil) {
string = @"";
}
if ([string isKindOfClass:[NSNumber class]]) {
NSLog(@"怎么回事,小兄弟,传个Number过来,邪门歪道,传统方法演变过来,偶尔有用");
string = string.description;
}
if ([string isKindOfClass:[NSString class]] && [string containsString:@"(null)"]) {
string = [string stringByReplacingOccurrencesOfString:@"(null)" withString:@""];
}
// 这里的self是UILabel,_cmd是setText:
objc_msgSend(self, @selector(setTextHooked:),string.description);
}
/// 没用,只是为了消除警告
- (void) setTextHooked:(NSString *)string {}
@end
第三种方式:
在看别人写方式2的时候,我就在想,不在类别中,但是还是想使用exchangeImplementations,经过一番尝试,发现是可以的。获取到原始方法的imp,通过imp调用原始方法,完成设置text.
#import <Foundation/Foundation.h>
@interface OtherHook : NSObject
@end
#import "OtherHook.h"
#import <objc/runtime.h>
#import <objc/message.h>
#import <UIKit/UIKit.h>
@implementation OtherHook
+ (void)load {
SEL origSel = @selector(setText:) ;
SEL altSel = @selector(setTextHooked:) ;
Method origMethod = class_getInstanceMethod([UILabel class], origSel);
Method altMethod = class_getInstanceMethod(self, altSel);
if (!origMethod || !altMethod) {
return ;
}
method_exchangeImplementations(origMethod,altMethod);
}
/// 主要是解决 服务器有时返回NSNumber类型,但是用了NSString指针接收,在 label.text = @(num) 时崩溃
- (void) setTextHooked:(NSString *)string {
if (string == nil) {
string = @"";
}
if ([string isKindOfClass:[NSNumber class]]) {
NSLog(@"怎么回事,小兄弟,传个Number过来,邪门歪道,偶尔有用");
string = string.description;
}
if ([string isKindOfClass:[NSString class]] && [string containsString:@"(null)"]) {
string = [string stringByReplacingOccurrencesOfString:@"(null)" withString:@""];
}
// 这里的self是UILabel,_cmd是setText:
// 这样不行,因为UILabel中没有这个setTextHooked:,所以会找不到方法崩溃
// [self setTextHooked:string.description] ;
// 这也不行,会递归
// objc_msgSend(self, @selector(setText:),string.description);
// 多次尝试这个才行,获取到原始方法的imp,通过imp调用原始方法,完成设置text
Method altMethod = class_getInstanceMethod([OtherHook class], @selector(setTextHooked:));
IMP origImp = method_getImplementation(altMethod);
origImp(self,_cmd,string.description);
}
@end
第四种方式:
可以使用category在类别中覆盖同样的方法,然后其他在调用setText的时候会优先调用到category中的方法,然后在category中处理完成之后再调用系统的setText方法.
#import <UIKit/UIKit.h>
@interface UILabel (SafeSetText)
@end
#import "UILabel+SafeSetText.h"
@import ObjectiveC.runtime;
@implementation UILabel (SafeSetText)
- (void)setText:(NSString *)text {
if (text == nil) {
text = @"";
}
if ([text isKindOfClass:[NSString class] ] == NO) {
text = text.description;
}
// 调用系统的原始方法,
// 1.从方法列表中倒序寻找,类别中的方法在最前面,如果正序寻找需要遍历完,倒序可以break
// 2.找到第一个匹配的就是系统的原始方法,
// 3.用原始方法的imp指针调用
u_int count = 0;
// class_copyMethodList只会找本类的方法,如果是UILabel的子类会找不到而崩溃,所以这里必须是[UILabel class]
Method * methods = class_copyMethodList([UILabel class], &count);
NSInteger index = -1;
for (int i = count-1; i>=0; i--) {
SEL tempSel = method_getName(methods[i]);
NSString * strName = [NSString stringWithCString:sel_getName(tempSel) encoding:NSUTF8StringEncoding];
NSLog(@"%d %@",i,strName);
if (tempSel == _cmd) {
index = i;
break;
}
}
NSAssert(index != -1, @"未找到指定的方法");
SEL resultSel = method_getName(methods[index]);
IMP imp = method_getImplementation(methods[index]);
( (void (*) (id,SEL,NSString *)) imp)(self,resultSel,text);
}
@end
上面的4种方法都可以实现,方法1是最好的。就像以前做几何证明题,不必局限于一种方式,完成证明有很多种方法,第2,3种方法算是一个思维的扩展。第2,3种方法在hook私有类的时候也可以奏效哦. 第4种方法可以作为一个调用类别中的原始方法的例子
注意:这4个方式有一个就行了,刚才养蛊一般留了4个,就不会起作用了,调用起来就复杂了,所以runtime确实是双刃剑啊。
最后附上git地址:https://github.com/guochaoshun/UILabel_SetText