zoukankan      html  css  js  c++  java
  • NSString 、NSDictionary、NSMutableArray 返回值为空时的处理方法

    #import "NSDictionary+safeDictionary.h"

     

    NSString * const kEmptyString = @"";

    @implementation NSDictionary (NSDictionary_Utils)

     // return an empty string if the value is null or not a string.

    - (NSString *)stringForKey:(id)key

    {

        NSString *result = [self objectForKey:key];

        if([result isKindOfClass:[NSString class]])

        {

            return result;

        }

        if ([result isKindOfClass:[NSNumber class]]) {

            return [NSString stringWithFormat:@"%@",result];

        }

        else {

            return kEmptyString;

        }

    }

     

    // return nil if the object is null or not a NSDictionary.

    - (NSDictionary *)dictionaryForKey:(id)key

    {

        NSDictionary *result = [self objectForKey:key];

        if([result isKindOfClass:[NSDictionary class]])

        {

            return result;

        }

        else {

            return nil;

        }

    }

     

    //  return nil if the object is null or not a NSArray.

    - (NSMutableArray *)arrayForKey:(id)key

    {

        NSMutableArray *result = [self objectForKey:key];

        if([result isKindOfClass:[NSArray class]])

        {

            return result;

        }

        else {

            return nil;

        }

    }

    1
  • 相关阅读:
    抽象工厂模式
    python 工厂方法
    采用__call__ 实现装饰器模式
    策略模式
    采集15个代理IP网站,打造免费代理IP池
    grid网格布局——色子布局
    观察者模式
    搭建免费代理池---采集代理(1)
    python 爬虫 user-agent 生成
    多进程 + 多线程抓取博客园信息
  • 原文地址:https://www.cnblogs.com/fantasy3588/p/4900711.html
Copyright © 2011-2022 走看看