NullSafe详细解读
来源:互联网 发布:mysql全文检索 编辑:程序博客网 时间:2024/05/24 00:47
NullSafe个人感觉就是神一般的存在
首先是github地址: https://github.com/nicklockwood/NullSafe
具体干神马用的了 ??? ios中网络编程解析json的时候,如果你很不幸遇到了PHP的接口或者遇到了水平不高的Java接口,那是蛋疼无比。。。空字符串是null,空对象是null ,甚至空的数组也是null , 先来段代码压压惊 。。
#import "ViewController.h"@interface NSData (NullTests)- (double)NullTestMethod;@end@implementation NSData (NullTests)- (double)NullTestMethod{ return 47.6;}@end@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; [self testStringValue]; NSLog(@"\n"); [self testFloatValue]; NSLog(@"\n"); [self testIntValue]; NSLog(@"\n"); [self testPointerValue]; NSLog(@"\n"); [self testClass]; NSLog(@"\n"); [self testDescription]; NSLog(@"\n"); [self testRange]; NSLog(@"\n"); [self testCategory]; }- (void)testStringValue{ @try{ id nullValue = [NSNull null]; NSString *result = [nullValue stringValue]; NSLog(@"testStringValue ============ %@ ",result); }@catch(NSException *e){ NSLog(@"testStringValue eee = %@",e); } }- (void)testFloatValue{ @try{ id nullValue = [NSNull null]; __unused float x = floorf(123.456f); float result = [nullValue floatValue]; NSLog(@"testFloatValue ============ %f ",result); }@catch(NSException *e){ NSLog(@"testFloatValue eee = %@",e); }}- (void)testIntValue{ @try{ id nullValue = [NSNull null]; int result = [nullValue intValue]; NSLog(@"testIntValue ============ %d ",result); }@catch(NSException *e){ NSLog(@"testIntValue eee = %@",e); }}- (void)testPointerValue{ @try{ id nullValue = [NSNull null]; const void *result = [nullValue bytes]; NSLog(@"testPointerValue ============ %p ",result); }@catch(NSException *e){ NSLog(@"testPointerValue eee = %@",e); }}- (void)testClass{ @try{ id nullValue = [NSNull null]; NSString *result = NSStringFromClass([nullValue class]); NSLog(@"testClass ============ %@ ",result); }@catch(NSException *e){ NSLog(@"testClass eee = %@",e); }}- (void)testDescription{ @try{ id nullValue = [NSNull null]; NSString *result = [nullValue description]; NSLog(@"testDescription ============ %@ ",result); }@catch(NSException *e){ NSLog(@"testClass eee = %@",e); }}- (void)testRange{ @try{ id nullValue = [NSNull null]; NSRange result = [nullValue range]; NSLog(@"testRange ============ %ld,%ld ",result.length,result.location); }@catch(NSException *e){ NSLog(@"testClass eee = %@",e); }}- (void)testCategory{ @try{ id nullValue = [NSNull null]; __unused double x = floor(123.456); double result = [nullValue NullTestMethod]; NSLog(@"testCategory ============ %lf ",result); }@catch(NSException *e){ NSLog(@"testClass eee = %@",e); }}2017-10-11 11:21:41.488769+0800 runtime[27970:7856366] -[NSNull stringValue]: unrecognized selector sent to instance 0x1a7a6fef82017-10-11 11:21:41.488915+0800 runtime[27970:7856366] testStringValue eee = -[NSNull stringValue]: unrecognized selector sent to instance 0x1a7a6fef82017-10-11 11:21:41.488965+0800 runtime[27970:7856366] -[NSNull floatValue]: unrecognized selector sent to instance 0x1a7a6fef82017-10-11 11:21:41.489018+0800 runtime[27970:7856366] testFloatValue eee = -[NSNull floatValue]: unrecognized selector sent to instance 0x1a7a6fef82017-10-11 11:21:41.489076+0800 runtime[27970:7856366] -[NSNull intValue]: unrecognized selector sent to instance 0x1a7a6fef82017-10-11 11:21:41.489155+0800 runtime[27970:7856366] testIntValue eee = -[NSNull intValue]: unrecognized selector sent to instance 0x1a7a6fef82017-10-11 11:21:41.489193+0800 runtime[27970:7856366] -[NSNull bytes]: unrecognized selector sent to instance 0x1a7a6fef82017-10-11 11:21:41.489229+0800 runtime[27970:7856366] testPointerValue eee = -[NSNull bytes]: unrecognized selector sent to instance 0x1a7a6fef82017-10-11 11:21:41.489264+0800 runtime[27970:7856366] testClass ============ NSNull2017-10-11 11:21:41.489344+0800 runtime[27970:7856366] testDescription ============ <null>2017-10-11 11:21:41.489380+0800 runtime[27970:7856366] -[NSNull range]: unrecognized selector sent to instance 0x1a7a6fef82017-10-11 11:21:41.489441+0800 runtime[27970:7856366] testClass eee = -[NSNull range]: unrecognized selector sent to instance 0x1a7a6fef82017-10-11 11:21:41.489544+0800 runtime[27970:7856366] -[NSNull NullTestMethod]: unrecognized selector sent to instance 0x1a7a6fef82017-10-11 11:21:41.489599+0800 runtime[27970:7856366] testClass eee = -[NSNull NullTestMethod]: unrecognized selector sent to instance 0x1a7a6fef8
我们发现,操作空对象的时候报各种异常,为了让程序稳定的执行,我将异常try catch处理了,但是按照ios程序猿,try catch用的很少,不像android开发动不动就try一下 ….而且我们发现都是因为对象空引起的异常,如果服务端数据合法合理这些都可以避免,关键是。。。。多少前端程序猿能遇到靠谱的服务端了 ?总之我没遇到几个。。
NullSafe 就要放大招了 。。。。。
你只需要将NullSafe拖入到你的项目中 ,不需要导入
重新运行代码,你会发现。。。。 异常捕获都不走了。。。
阅读全文
0 0
- NullSafe详细解读
- nullsafe
- NullSafe原理
- 详细解读java IO
- 详细解读statspack报告
- java详细解读
- makefile 详细解读
- sizeof详细解读
- Java API详细解读
- 详细解读 STATSPACK 报告
- 详细解读 STATSPACK 报告
- 详细解读_STATSPACK_报告
- uboot详细解读
- Ehcache详细解读
- free_page_tables图文并茂详细解读
- 命令模式详细解读。
- ehcache详细解读
- UBOOT详细解读
- 权限管理命令chown
- 机器学习必备手册
- SpringMVC学习笔记
- javascript 实现页面间传值(2)
- Mybatis 二级缓存
- NullSafe详细解读
- rlwrap 安装应用
- 中国首届原型设计大赛在成都举办
- MySQL-02-windows下查看frm,myi,myd
- log4j2应用详解
- 使用Toad调用expdp数据泵备份数据步骤及注意事项
- linux 学习系列-Linux 用户和用户组管理
- 权限管理命令chgrp
- WebAPI 返回Html,js