NullSafe详细解读

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 0x1a7a6fef8
2017-10-11 11:21:41.488915+0800 runtime[27970:7856366] testStringValue eee = -[NSNull stringValue]: unrecognized selector sent to instance 0x1a7a6fef8

2017-10-11 11:21:41.488965+0800 runtime[27970:7856366] -[NSNull floatValue]: unrecognized selector sent to instance 0x1a7a6fef8
2017-10-11 11:21:41.489018+0800 runtime[27970:7856366] testFloatValue eee = -[NSNull floatValue]: unrecognized selector sent to instance 0x1a7a6fef8

2017-10-11 11:21:41.489076+0800 runtime[27970:7856366] -[NSNull intValue]: unrecognized selector sent to instance 0x1a7a6fef8
2017-10-11 11:21:41.489155+0800 runtime[27970:7856366] testIntValue eee = -[NSNull intValue]: unrecognized selector sent to instance 0x1a7a6fef8

2017-10-11 11:21:41.489193+0800 runtime[27970:7856366] -[NSNull bytes]: unrecognized selector sent to instance 0x1a7a6fef8
2017-10-11 11:21:41.489229+0800 runtime[27970:7856366] testPointerValue eee = -[NSNull bytes]: unrecognized selector sent to instance 0x1a7a6fef8

2017-10-11 11:21:41.489264+0800 runtime[27970:7856366] testClass ============ NSNull

2017-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 0x1a7a6fef8
2017-10-11 11:21:41.489441+0800 runtime[27970:7856366] testClass eee = -[NSNull range]: unrecognized selector sent to instance 0x1a7a6fef8

2017-10-11 11:21:41.489544+0800 runtime[27970:7856366] -[NSNull NullTestMethod]: unrecognized selector sent to instance 0x1a7a6fef8
2017-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拖入到你的项目中 ,不需要导入
重新运行代码,你会发现。。。。 异常捕获都不走了。。。
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值