• iOS 增强程序健壮性


      在项目开发中,和服务端交互数据时,若服务端数据为空时,会出现 <null>,客户端解析时会 Crash,为了增强程序的健壮性,减少 Crash 的发生,可以使用 NullSafe 这个类别。它对不识别的类型返回 nil,而不是抛出异常,它减少了例如因为 JSON 解析中 数组或字符串为 null 时导致的 Crash。这些异常对客户端来说是不可预期的。

      使用时只需要把 NullSafe.m 文件拖进工程就可以了,它在程序运行时自动加载,你不需要再导入其他头文件了。

      如果想要禁止 NullSafe 的话,需要设置:NULLSAFE_ENABLED=0,或者在 .pch 文件中添加:

    #ifdef DEBUG
    #define NULLSAFE_ENABLED 0
    #endif

      附:NullSafe 源码:

    #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, 0);
                    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 = 0; 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

      

  • 相关阅读:
    共用体
    建立动态链表
    动态分配储存与链表
    结构指针变量作函数参数
    R语言实战 第7章
    R-6 线性回归模型流程
    R-5 相关分析-卡方分析
    R-4 方差分析
    R-3 t分布--t置信区间--t检验
    R-2
  • 原文地址:https://www.cnblogs.com/ZachRobin/p/7245582.html
Copyright © 2020-2023  润新知