提供一个解决unrecognized selector错误的宏定义,所有类都不会触发unrecognized selector错误 —
来源:互联网 发布:php手机网页游戏源码 编辑:程序博客网 时间:2024/05/14 02:45
Unrecognized selector
在调用performSelector时如果忘记了检查selector是否存在(respondsToSelector),则很容易造成unrecognized selector exception, 这里提供一个简单方便的方案解决unrecognized selector报错的问题
NSObject+NoUnrecognizedSelector.h
#import <Foundation/Foundation.h>@interface NSObject (NoUnrecognizedSelector)@end
NSObject+NoUnrecognizedSelector.m
#import "NSObject+NoUnrecognizedSelector.h"#import <objc/runtime.h>@implementation NSObject (NoUnrecognizedSelector)-(id)emptyMethod:(SEL) sel { printf("unrecoginzed selector occurs!(class = %s, method = %s)\n\n", class_getName([self class]), sel_getName(sel)); return nil;}- (void)forwardInvocation:(NSInvocation *)anInvocation { if (![self respondsToSelector:anInvocation.selector]) { SEL sel = [anInvocation selector]; anInvocation.selector = @selector(emptyMethod:); [anInvocation setArgument:&sel atIndex:2]; [anInvocation invoke]; }}- (id)forwardingTargetForSelector:(SEL)aSelector { return self;}- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector { NSMethodSignature * signature = nil; Class actualClass = object_getClass(self); Method method = class_getInstanceMethod(actualClass, aSelector); if (method) {char const *encoding = method_getTypeEncoding(method);signature = [NSMethodSignature signatureWithObjCTypes:encoding]; } else { method = class_getClassMethod(actualClass, aSelector); } if (method) { char const *encoding = method_getTypeEncoding(method);signature = [NSMethodSignature signatureWithObjCTypes:encoding]; } else { signature = [self methodSignatureForSelector:@selector(emptyMethod:)]; } return signature;}@end
0 0
- 提供一个解决unrecognized selector错误的宏定义,所有类都不会触发unrecognized selector错误 —
- unrecognized selector的错误问题
- "unrecognized selector sent to instance *****"的错误
- -[__NSArrayI addObjectsFromArray:]: unrecognized selector错误
- 什么时候会报unrecognized selector错误?
- xcode object-c unrecognized selector错误解析
- 编辑符号断点,断点unrecognized selector错误
- swift-错误unrecognized selector sent to instance
- iOS unrecognized selector sent to class 错误
- 发生错误unrecognized selector sent to instance的原因
- iOS中变量命名引起的错误错误: [UILabel copyWithZone:]: unrecognized selector sent to instance
- 错误: [UILabel copyWithZone:]: unrecognized selector sent to instance
- 错误:[__NSCFString md5Hash]: unrecognized selector sent to instance 0x223120'
- iOS开发笔记--[__NSArrayI addObjectsFromArray:]: unrecognized selector错误
- iOS开发之unrecognized selector sent to instance错误
- 关于reactivecocoa出现 unrecognized selector sent to instance错误
- [UIView numberofRowsInSection]unrecognized selector sent to instance 0x1808bef0 错误
- iOS 【错误:unrecognized selector sent to instance 0x7aa552b0】
- 剑指offer(四十二)之左旋转字符串
- linux编程——文件操作1
- Adafruit公司2015年最棒的开源可穿戴设备
- Android之系统服务-WindowManager
- 乐学成语——第一部分
- 提供一个解决unrecognized selector错误的宏定义,所有类都不会触发unrecognized selector错误 —
- 汇编程序员之代码风格指南
- redis-集群重启
- wxpython实现图灵机器人的GUI-Python
- filename.whl is not supported wheel on this platform
- 回忆多项式解析运算思路
- Bootstrap是什么,Bootstrap里的文件分别表示什么?都有什么用?
- JDBC的简介及操作
- Product of Array Except Self