在iOS开发过程中经常需要与服务器进行数据通讯,JSON就是一种常用的高效简洁的数据格式。

问题:

在项目中,一直遇到一个坑的问题,程序在获取某些数据之后莫名崩溃。原因是:由于服务器的数据库中有些字段为空,然后以JSON形式返回给客户端时就会出现这样的数据:repairs = "<null>"

这个数据类型不是nil 也不是 String。 解析成对象之后,如果直接向这个对象发送消息(eg:length,count 等等)就会直接崩溃。提示错误为:

-[NSNull length]: unrecognized selector sent to instance

解决方案:

用了一个Category,叫做NullSafe 。

NullSafe思路:在运行时操作,把这个讨厌的空值置为nil,而nil是安全的,可以向nil对象发送任何message而不会奔溃。这个category使用起来非常方便,只要加入到了工程中就可以了,你其他的什么都不用做,很简单。

NullSafe 源码:

  1. #import <objc/runtime.h>
  2. #import <Foundation/Foundation.h>
  3.  
  4. #ifndef NULLSAFE_ENABLED
  5. #define NULLSAFE_ENABLED 1
  6. #endif
  7.  
  8. #pragma GCC diagnostic ignored "-Wgnu-conditional-omitted-operand"
  9.  
  10. @implementation NSNull (NullSafe)
  11.  
  12. #if NULLSAFE_ENABLED
  13.  
  14. - (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
  15. {
  16. @synchronized([self class])
  17. {
  18. //look up method signature
  19. NSMethodSignature *signature = [super methodSignatureForSelector:selector];
  20. if (!signature)
  21. {
  22. //not supported by NSNull, search other classes
  23. static NSMutableSet *classList = nil;
  24. static NSMutableDictionary *signatureCache = nil;
  25. if (signatureCache == nil)
  26. {
  27. classList = [[NSMutableSet alloc] init];
  28. signatureCache = [[NSMutableDictionary alloc] init];
  29.  
  30. //get class list
  31. int numClasses = objc_getClassList(NULL, 0);
  32. Class *classes = (Class *)malloc(sizeof(Class) * (unsigned long)numClasses);
  33. numClasses = objc_getClassList(classes, numClasses);
  34.  
  35. //add to list for checking
  36. NSMutableSet *excluded = [NSMutableSet set];
  37. for (int i = 0; i < numClasses; i++)
  38. {
  39. //determine if class has a superclass
  40. Class someClass = classes[i];
  41. Class superclass = class_getSuperclass(someClass);
  42. while (superclass)
  43. {
  44. if (superclass == [NSObject class])
  45. {
  46. [classList addObject:someClass];
  47. break;
  48. }
  49. [excluded addObject:NSStringFromClass(superclass)];
  50. superclass = class_getSuperclass(superclass);
  51. }
  52. }
  53.  
  54. //remove all classes that have subclasses
  55. for (Class someClass in excluded)
  56. {
  57. [classList removeObject:someClass];
  58. }
  59.  
  60. //free class list
  61. free(classes);
  62. }
  63.  
  64. //check implementation cache first
  65. NSString *selectorString = NSStringFromSelector(selector);
  66. signature = signatureCache[selectorString];
  67. if (!signature)
  68. {
  69. //find implementation
  70. for (Class someClass in classList)
  71. {
  72. if ([someClass instancesRespondToSelector:selector])
  73. {
  74. signature = [someClass instanceMethodSignatureForSelector:selector];
  75. break;
  76. }
  77. }
  78.  
  79. //cache for next time
  80. signatureCache[selectorString] = signature ?: [NSNull null];
  81. }
  82. else if ([signature isKindOfClass:[NSNull class]])
  83. {
  84. signature = nil;
  85. }
  86. }
  87. return signature;
  88. }
  89. }
  90.  
  91. - (void)forwardInvocation:(NSInvocation *)invocation
  92. {
  93. invocation.target = nil;
  94. [invocation invoke];
  95. }
  96.  
  97. #endif
  98.  
  99. @end

详细的请去Github上查看:

https://github.com/nicklockwood/NullSafe

iOS之数据解析时<null>的处理的更多相关文章

  1. iOS - JSON 数据解析

     iOS - JSON 数据解析 前言 NS_CLASS_AVAILABLE(10_7, 5_0) @interface NSJSONSerialization : NSObject @availab ...

  2. iOS - XML 数据解析

    前言 @interface NSXMLParser : NSObject public class NSXMLParser : NSObject 1.XML 数据 XML(Extensible Mar ...

  3. iOS - Plist 数据解析

    前言 NS_AVAILABLE(10_6, 4_0) @interface NSPropertyListSerialization : NSObject 如果对象是 NSArray 或 NSDicti ...

  4. 浅议iOS网络数据解析

    /*------------------------------------ 数据解析: 1.JSON数据 --------------------------------*/ 重点:1.什么是JSO ...

  5. IOS 请求数据解析 XML 和 JSON

    好久没写文章了,回忆一下以前的内容记录一下吧. 这一段主要接触的就是数据解析,就说一下数据解析 现在数据解析一般解析两种数据 xml 和 JSON 那就从xml解析说起吧 xml解析需要用到一个类 N ...

  6. iOS UI13_数据解析XML_,JSON

    - (IBAction)parserButton:(id)sender { parserXML *parser =[[parserXML alloc] init]; [parser startPars ...

  7. IOS - JSON数据解析 小3种方法

    [manager GET:serverURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject ...

  8. iOS开发——数据解析Swift篇&简单json数据处理

    简单json数据处理 //loadWeather var url = NSURL(string: "http://www.weather.com.cn/adat/sk/101240701.h ...

  9. ios中json解析出现的null问题

    http://my.oschina.net/iq19900204/blog/408034 在iOS开发过程中经常需要与服务器进行数据通讯,Json就是一种常用的高效简洁的数据格式. 问题现象 但是几个 ...

随机推荐

  1. 【腾讯Bugly经验分享】程序员的成长离不开哪些软技能?

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57ce8068d4d44a246f72baf2 Dev Club 是一个交流移动 ...

  2. WPF - 属性系统 - APaas(AttachedProperty as a service)

    是的,文章的题目看起来很牛,我承认. 附加属性是WPF中的一个非常重要的功能.例如在设置布局的过程中,软件开发人员就常常通过DockPanel的Dock附加属性来设置其各个子元素所处的布局位置.同样地 ...

  3. "用户增长"--快速身份认证实现用户增长的技术和产品方案

    "用户增长"--快速身份认证实现用户增长的技术和产品方案 1   引言 作为一个互联网产品,用户量的增长是一个非常重要的衡量指标. 这是一个集合了销售,市场,运营,技术的综合能力. ...

  4. iOS开发系列--数据存取

    概览 在iOS开发中数据存储的方式可以归纳为两类:一类是存储为文件,另一类是存储到数据库.例如前面IOS开发系列-Objective-C之Foundation框架的文章中提到归档.plist文件存储, ...

  5. AWS的SysOps认证考试样题解析

    刚考过了AWS的developer认证,顺手做了一下SysOps的样题.以下是题目和答案. When working with Amazon RDS, by default AWS is respon ...

  6. ABP源码分析四十三:ZERO的本地化

    ABP Zero模块扩展了ABP基础框架中的本地化功能,实现了通过数据库管理本地化的功能.其通过数据库保存本地化语言及其资源. ApplicationLanguage:代表本地化语言的实体类.一种语言 ...

  7. jQuery的几个应例题、JSON基础

    1.下拉列表取值.赋值 (1)写个下拉列表,如下: <select id="sel"> <option value="山东">山东< ...

  8. 04.移动先行之谁主沉浮----XAML的探索

    如果移动方向有任何问题请参考===> 异常处理汇总-移动系列(点) 移动先行之谁主沉浮? 带着你的Net飞奔吧! 链接======>(点) XMAL引入 XAML 类似于 HTML,是一种 ...

  9. [SQL] SQL 基础知识梳理(一)- 数据库与 SQL

    SQL 基础知识梳理(一)- 数据库与 SQL [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5902856.html 目录 What's 数据库 ...

  10. 【热文】 为什么很多硅谷工程师偏爱 OS X,而不是 Linux 或 Windows?

    校对:伯乐在线 - 黄利民 链接: 1. Why do most of the developers in Silicon Valley prefer OS X over Linux or Windo ...