用类别重写系统API的方法,去除xcode警告
来源:互联网 发布:美图美妆软件 编辑:程序博客网 时间:2024/05/16 14:22
当你用一个类别来重写系统的方法时候
比如:
+ (UIFont *)systemFontOfSize:(CGFloat)fontSize {return [self aCustomFontOfSize:fontSize];}
xcode会提示你 "Category is implementing a method which will also be implemented by its primary class".
去除这种警告的几种方法
1.
#pragma clang diagnostic push#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"// do your override#pragma clang diagnostic pop
2.
better alternative (see bneely's answer to why this warning is saving you from disaster) is to use method swizzling. By using method swizzling, you can replace an existing method from a category without the uncertainty of who "wins", and while preserving the ability to call through to the old method. The secret is to give the override a different method name, then swap them using runtime functions.
#import <objc/runtime.h>#import <objc/message.h>void MethodSwizzle(Class c, SEL orig, SEL new) { Method origMethod = class_getInstanceMethod(c, orig); Method newMethod = class_getInstanceMethod(c, new); if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))) class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod)); else method_exchangeImplementations(origMethod, newMethod);}
Then define your custom implementation:
+ (UIFont *)mySystemFontOfSize:(CGFloat)fontSize {...}
Override the default implementation with yours:
MethodSwizzle([UIFont class], @selector(systemFontOfSize:), @selector(mySystemFontOfSize:));
3.
Try this in your code:
+(void)load{ EXCHANGE_METHOD(Method1, Method1Impl);}
UPDATE2: Add this macro
#import <Foundation/Foundation.h>#define EXCHANGE_METHOD(a,b) [[self class]exchangeMethod:@selector(a) withNewMethod:@selector(b)]@interface NSObject (MethodExchange)+(void)exchangeMethod:(SEL)origSel withNewMethod:(SEL)newSel;@end#import <objc/runtime.h>@implementation NSObject (MethodExchange)+(void)exchangeMethod:(SEL)origSel withNewMethod:(SEL)newSel{ Class class = [self class]; Method origMethod = class_getInstanceMethod(class, origSel); if (!origMethod){ origMethod = class_getClassMethod(class, origSel); } if (!origMethod) @throw [NSException exceptionWithName:@"Original method not found" reason:nil userInfo:nil]; Method newMethod = class_getInstanceMethod(class, newSel); if (!newMethod){ newMethod = class_getClassMethod(class, newSel); } if (!newMethod) @throw [NSException exceptionWithName:@"New method not found" reason:nil userInfo:nil]; if (origMethod==newMethod) @throw [NSException exceptionWithName:@"Methods are the same" reason:nil userInfo:nil]; method_exchangeImplementations(origMethod, newMethod);}@end
- 用类别重写系统API的方法,去除xcode警告
- 去除xcode的编译警告
- 强制去除Xcode的警告
- 强制去除xcode的编译警告
- 强制去除xcode的编译警告
- 强制去除xcode的编译警告
- 强制去除xcode的编译警告
- 强制去除xcode的编译警告
- 强制去除xcode的编译警告
- 强制去除xcode的编译警告
- 强制去除xcode的编译警告
- 如何强制去除xcode的编译警告
- 强制去除xcode的编译警告
- 强制去除xcode的编译警告
- 强制去除xcode的编译警告
- Xcode怎样去除警告
- 强制去除Xcode警告
- 常用去除警告的方法
- 关于gridView里面点击后图片变小的原因
- C# Office开发 隐藏书签符号
- Git忽略文件
- 程序员如何保持优秀
- 经典收藏 50个jQuery Mobile开发技巧集萃
- 用类别重写系统API的方法,去除xcode警告
- [编译环境] pkg-config for mac 安装
- android – 多屏幕适配相关
- ubuntu 安装 nginx以及apache安装配置
- 使用 PIVOT 和 UNPIVOT
- Unity3D Shader官方教程翻译(四)----Shader语法:SubShader
- Unity3D Shader官方教程翻译(五)----Shader语法:Pass
- socket套接字选项列表
- Rational Rose和PowerDesigner两款建模工具分析比较