iOS 增强程序健壮性 - - 使用 NullSafe 对 <null> 处理
在项目开发中,和服务端交互数据时,若服务端数据为空时,会出现 <null>,客户端解析时会 Crash,为了增强程序的健壮性,减少 Crash 的发生,可以使用 NullSafe 这个类别。它对不识别的类型返回 nil,而不是抛出异常,它减少了例如因为 JSON 解析中 数组或字符串为 null 时导致的 Crash。这些异常对客户端来说是不可预期的。
使用时只需要把 NullSafe.m 文件拖进工程就可以了,它在程序运行时自动加载,你不需要再导入其他头文件了。
如果想要禁止 NullSafe 的话,需要设置:NULLSAFE_ENABLED=0,或者在 .pch 文件中添加:
- #ifdef DEBUG
- #define NULLSAFE_ENABLED 0
- #endif
- #import <objc/runtime.h>
- #import <Foundation/Foundation.h>
- #ifndef NULLSAFE_ENABLED
- #define NULLSAFE_ENABLED 1
- #endif
- #pragma GCC diagnostic ignored "-Wgnu-conditional-omitted-operand"
- @implementation NSNull (NullSafe)
- #if NULLSAFE_ENABLED
- - (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
- {
- @synchronized([self class])
- {
- //look up method signature
- NSMethodSignature *signature = [super methodSignatureForSelector:selector];
- if (!signature)
- {
- //not supported by NSNull, search other classes
- static NSMutableSet *classList = nil;
- static NSMutableDictionary *signatureCache = nil;
- if (signatureCache == nil)
- {
- classList = [[NSMutableSet alloc] init];
- signatureCache = [[NSMutableDictionary alloc] init];
- //get class list
- int numClasses = objc_getClassList(NULL, );
- Class *classes = (Class *)malloc(sizeof(Class) * (unsigned long)numClasses);
- numClasses = objc_getClassList(classes, numClasses);
- //add to list for checking
- NSMutableSet *excluded = [NSMutableSet set];
- for (int i = ; i < numClasses; i++)
- {
- //determine if class has a superclass
- Class someClass = classes[i];
- Class superclass = class_getSuperclass(someClass);
- while (superclass)
- {
- if (superclass == [NSObject class])
- {
- [classList addObject:someClass];
- break;
- }
- [excluded addObject:NSStringFromClass(superclass)];
- superclass = class_getSuperclass(superclass);
- }
- }
- //remove all classes that have subclasses
- for (Class someClass in excluded)
- {
- [classList removeObject:someClass];
- }
- //free class list
- free(classes);
- }
- //check implementation cache first
- NSString *selectorString = NSStringFromSelector(selector);
- signature = signatureCache[selectorString];
- if (!signature)
- {
- //find implementation
- for (Class someClass in classList)
- {
- if ([someClass instancesRespondToSelector:selector])
- {
- signature = [someClass instanceMethodSignatureForSelector:selector];
- break;
- }
- }
- //cache for next time
- signatureCache[selectorString] = signature ?: [NSNull null];
- }
- else if ([signature isKindOfClass:[NSNull class]])
- {
- signature = nil;
- }
- }
- return signature;
- }
- }
- - (void)forwardInvocation:(NSInvocation *)invocation
- {
- invocation.target = nil;
- [invocation invoke];
- }
- #endif
- @end
iOS 增强程序健壮性 - - 使用 NullSafe 对 <null> 处理的更多相关文章
- Python 单元测试 增强系统健壮性
问题背景交代 注意,JulyNovel只爬取免费小说,所有vip章节全部导航至起点网站,遵循robots协议,所有数据仅供学习用途,侵删 通过编写单元测试,提高JulyNovel系统可靠性 首先我们知 ...
- 程序的健壮性Robustness
所谓的程序健壮性是指处理异常的能力,在异常中能够独立处理异常,并且把正确的答案输出. 例如: 有一个程序能够下载一个文件到指定的路径,但是这个路径是不存在的,因此程序必须要处理这个情况. 例1:下面的 ...
- 25个增强iOS应用程序性能的提示和技巧(高级篇)(2)
25个增强iOS应用程序性能的提示和技巧(高级篇)(2) 2013-04-16 14:56 破船之家 beyondvincent 字号:T | T 在开发iOS应用程序时,让程序具有良好的性能是非常关 ...
- 25个增强iOS应用程序性能的提示和技巧(高级篇)(1)
25个增强iOS应用程序性能的提示和技巧(高级篇)(1) 2013-04-16 14:56 破船之家 beyondvincent 字号:T | T 在开发iOS应用程序时,让程序具有良好的性能是非常关 ...
- 25个增强iOS应用程序性能的提示和技巧(中级篇)(3)
25个增强iOS应用程序性能的提示和技巧(中级篇)(3) 2013-04-16 14:42 破船之家 beyondvincent 字号:T | T 本文收集了25个关于可以提升程序性能的提示和技巧,分 ...
- 25个增强iOS应用程序性能的提示和技巧(中级篇)(2)
25个增强iOS应用程序性能的提示和技巧(中级篇)(2) 2013-04-16 14:42 破船之家 beyondvincent 字号:T | T 本文收集了25个关于可以提升程序性能的提示和技巧,分 ...
- 25个增强iOS应用程序性能的提示和技巧--中级篇
25个增强iOS应用程序性能的提示和技巧--中级篇 标签: ios性能优化内存管理 2013-12-13 10:55 738人阅读 评论(0) 收藏 举报 分类: IPhone开发高级系列(34) ...
- 25个增强iOS应用程序性能的提示和技巧(初级篇)
25个增强iOS应用程序性能的提示和技巧(初级篇) 标签: ios内存管理性能优化 2013-12-13 10:53 916人阅读 评论(0) 收藏 举报 分类: IPhone开发高级系列(34) ...
- 25个增强iOS应用程序性能的提示和技巧 — 中级篇
本文由破船译自:raywenderlich 转载请注明出处:BeyondVincent的博客 _____________ 在开发iOS应用程序时.让程序具有良好的性能是非常关键的.这也是用户所期望的. ...
随机推荐
- arcgis中的Join(合并连接)和Relate(关联连接)
arcgis中的Join(合并连接)和Relate(关联连接) 一.区别 1.连接关系不一样. Relate(关联连接)方式连接的两个表之间的记录可以是“一对一”.“多对一”.“一对多”的关系 Joi ...
- DRF分页
一.序列化 from rest_framework impost serializers from . models import * class GoodsSerializer(serializer ...
- SQL更新语句的执行
联系上文SQL查询语句的执行 查询语句的那一套流程,更新语句也是同样会走一遍. 更新流程还涉及两个重要的日志模块: redo log(重做日志)和 binlog(归档日志) redo log:重做 ...
- .h头文件 .lib库文件 .dll动态链接库文件关系
.h头文件是编译时必须的,lib是链接时需要的,dll是运行时需要的. 附加依赖项的是.lib不是.dll,若生成了DLL,则肯定也生成 LIB文件.如果要完成源代码的编译和链接,有头文件和lib就够 ...
- 威佐夫博奕(Wythoff Game)poj 1067
有两堆各若干个物品,两个人轮流从某一堆或同时从两堆中取同样多的物品,规定每次至少取一个,多者不限,最后取光者得胜. 这种情况下是颇为复杂的.我们用(ak,bk)(ak ≤ bk ,k=0,1,2,…, ...
- 基于XML装配bean的解析-Bean的作用域
一.Bean的种类1.普通bean:<bean id="" class="A"> ,spring直接创建A实例,并返回. 2.FactoryBe ...
- chomp/undef/标量 --Perl 入门第二章
1.chomp 用途:去掉字符串 末尾的换行符 $text="a line of text \n" chomp($text) #去除行末的换行符 chomp() --本质上是一个 ...
- js + jquery 实现分页区翻页
简单来说,情况是这样的,假如做好了对动漫每一集进行分页,如下图: 但当分页太多就会变得不能看,而且前后箭头也不能只是摆设. 想要得到类似这样效果: 网上搜了一会翻页相关的库没什么效果,也不太合适自己的 ...
- Java连载82-Set、Collection、List、Map的UML演示
一.UML演示Collection集合的继承结构图 二.Set集合 1.List存储元素的特点:有序可重复.有序,存进去是什么顺序,拿出来还是什么顺序. 2.Set存储元素的特点:无序不可重复,存进去 ...
- selenium Python实现附件上传
对于web页面的上传功能一般有两类实现方式:一类是将本地文件的路径作为一个值放在input标签中,通过form表单将这个值提交给服务器:另一个类是插件上传,一般基于flash/javascript或者 ...