NSDictionary+BBASafeValue.h 7.43 KB
//
//  NSDictionary+BBAValue.h
//  BBAFoundation
//
//  Created by Zhu,Yusong on 2018/9/25.
//  Copyright © 2018年 Baidu. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "BDPFoundationDefines.h"

/*
 * 安全获取字典指定String类型的key下的value值的辅助方法,可以用在json字典的安全操作
 * 详细介绍请看README-SafeAPI
 */

/**
 * 通过keyPath,以及指定的类型前缀(该宏目前支持类型有:string、number、array、dictionary),从字典中获取对应类型的value
 * 如:
 * dic = @{@"k1": @{@"k2": @"xxxx", @"k3": @123}};
 * NSString *value = BBASafeDicKeyPath(dic, @"k1.k2", string)
 * NSNumber *value2 = BBASafeDicKeyPath(dic, @"k1.k3", number)
 */
#define BBASafeDicKeyPath(dic, keyPath, typePrefix) [dic bba_##typePrefix##ValueForKeyPath:keyPath]

/**
 * 通过key,以及指定的类型前缀(该宏目前支持类型有:string、number、array、dictionary),从字典中获取对应类型的value
 * 如:
 * dic = @{@"k1": @123, @"k2": @"xxxxx"};
 * NSNumber *value = BBASafeDic(dic, @"k1", number)
 * NSString *value2 = BBASafeDic(dic, @"k2", string)
 */
#define BBASafeDic(dic, key, typePrefix) [dic bba_##typePrefix##ValueForKey:key]

NS_ASSUME_NONNULL_BEGIN

@interface NSDictionary (BBASafeValue)


/**
 * @brief 安全获取指定key下的object值,与 safe_objectForKey 区别在于key值必须为NSString
 *
 * @param key string
 * @return id 如果key不为字符串,返回nil值
 */
- (nullable id)bba_objectForKey:(NSString *)key BDP_FOUNDATION_DEPRECATED("please use bdp_safeObjectForStringKey:");
- (nullable id)bdp_safeObjectForStringKey:(NSString *)key;


/**
 * @brief 安全获取指定key下的string值
 *
 * @param key string
 * @return NSString value 获取不到string则返回nil
 *
 * @note 相当于 bba_stringValueForKey: numberStringInsensitive:YES
 */
- (nullable NSString *)bba_stringValueForKey:(NSString *)key BDP_FOUNDATION_DEPRECATED("please use bdp_safeStringValueForKey:");
- (nullable NSString *)bdp_safeStringValueForKey:(NSString *)key;

/**
 * @brief 安全获取指定key下的string值
 *
 * @param key string
 * @param insensitive 对Number是否敏感,若为YES,则将Number转化成String返回
 * @return NSString value 获取不到string则返回nil
 */
- (nullable NSString *)bba_stringValueForKey:(NSString *)key numberStringInsensitive:(BOOL)insensitive BDP_FOUNDATION_DEPRECATED("please use bdp_safeStringValueForKey:numberStringInsensitive:");
- (nullable NSString *)bdp_safeStringValueForKey:(NSString *)key numberStringInsensitive:(BOOL)insensitive;

/**
 * @brief 安全获取指定key下的number值
 *
 * @param key string
 * @return NSNumber value 获取不到number则返回nil
 *
 * @note 相当于 bba_numberValueForKey: numberStringInsensitive:YES
 */
- (nullable NSNumber *)bba_numberValueForKey:(NSString *)key BDP_FOUNDATION_DEPRECATED("please use bdp_safeNumberValueForKey:");
- (nullable NSNumber *)bdp_safeNumberValueForKey:(NSString *)key;

/**
 * @brief 安全获取指定key下的number值
 *
 * @param key string
 * @param insensitive insensitive 对NumberString是否敏感,若为YES,则尝试将String转化成Number返回
 * @return NSNumber value 获取不到number则返回nil
 */
- (nullable NSNumber *)bba_numberValueForKey:(NSString *)key numberStringInsensitive:(BOOL)insensitive BDP_FOUNDATION_DEPRECATED("please use bdp_safeNumberValueForKey:numberStringInsensitive:");
- (nullable NSNumber *)bdp_safeNumberValueForKey:(NSString *)key numberStringInsensitive:(BOOL)insensitive;

/**
 * @brief 安全获取指定key下的date值
 *
 * @param key string
 * @return NSDate value 获取不到Date则返回nil
 */
- (nullable NSDate *)bba_dateValueForKey:(NSString *)key BDP_FOUNDATION_DEPRECATED("please use bdp_safeDateValueForKey:");
- (nullable NSDate *)bdp_safeDateValueForKey:(NSString *)key;

/**
 * @brief 安全获取指定key下的数组
 *
 * @param key string
 * @return NSArray value 获取不到数组则返回nil
 */
- (nullable NSArray *)bba_arrayValueForKey:(NSString *)key BDP_FOUNDATION_DEPRECATED("please use bdp_safeArrayValueForKey:");
- (nullable NSArray *)bdp_safeArrayValueForKey:(NSString *)key;

/// 获取指定key下的类型为字典的value,获取规则:
/// 1,若key对应的value是一个字典类型,则直接返回value;
/// 2,反之,返回nil
/**
 * @brief 安全获取指定key下的字典
 *
 * @param key string
 * @return NSDictionary value 获取不到字典则返回nil
 */
- (nullable NSDictionary *)bba_dictionaryValueForKey:(NSString *)key BDP_FOUNDATION_DEPRECATED("please use bdp_safeDictionaryValueForKey:");
- (nullable NSDictionary *)bdp_safeDictionaryValueForKey:(NSString *)key;

/**
 * @brief 安全取得连续取多层key对应的value 类似dic[@"info"][@"name"][@"surname"]
 *
 * @param keysArray 必须是NSString组成的数组
 * @return id 获取不到或者keysArray非法则返回nil
 */
- (nullable id)bba_objectForKeys:(NSArray*)keysArray BDP_FOUNDATION_DEPRECATED("please use bdp_safeObjectForKeys:");
- (nullable id)bdp_safeObjectForKeys:(NSArray*)keysArray;

#pragma mark - KeyPath

/**
 * @brief 通过指定的keyPath获取字符串类型的value
 *
 * @param keyPath 待获取的值的keyPath
 * @return 返回keyPath所指的字符串value(若keyPath所指的value不是字符串类型,则返回nil; 若value是NSNumber类型,则会自动转换为对应的字符串)
 */
- (nullable NSString *)bba_stringValueForKeyPath:(NSString *)keyPath BDP_FOUNDATION_DEPRECATED("please use bdp_safeStringValueForKeyPath:");
- (nullable NSString *)bdp_safeStringValueForKeyPath:(NSString *)keyPath;

/**
 * @brief 通过指定的keyPath获取数字类型的value
 *
 * @param keyPath 待获取的值的keyPath
 * @return 返回keyPath所指的数字value(若keyPath所指的value不是数字类型,则返回nil; 若value是NSString类型,且只包含数字,则会自动转换为对应的数字)
 */
- (nullable NSNumber *)bba_numberValueForKeyPath:(NSString *)keyPath BDP_FOUNDATION_DEPRECATED("please use bdp_safeNumberValueForKeyPath:");
- (nullable NSNumber *)bdp_safeNumberValueForKeyPath:(NSString *)keyPath;

/**
 * @brief 通过指定的keyPath获取array类型的value
 *
 * @param keyPath 待获取的值的keyPath
 * @return 返回keyPath所指的数组value(若keyPath所指的value不是array类型,则返回nil)
 */
- (nullable NSArray *)bba_arrayValueForKeyPath:(NSString *)keyPath BDP_FOUNDATION_DEPRECATED("please use bdp_safeArrayValueForKeyPath:");
- (nullable NSArray *)bdp_safeArrayValueForKeyPath:(NSString *)keyPath;

/**
 * @brief 通过指定的keyPath获取dictionary类型的value
 *
 * @param keyPath 待获取的值的keyPath
 * @return 返回keyPath所指的字典value(若keyPath所指的value不是dictionary类型,则返回nil)
 */
- (nullable NSDictionary *)bba_dictionaryValueForKeyPath:(NSString *)keyPath BDP_FOUNDATION_DEPRECATED("please use bdp_safeDictionaryValueForKeyPath:");
- (nullable NSDictionary *)bdp_safeDictionaryValueForKeyPath:(NSString *)keyPath;

/**
 * @brief 通过指定的keyPath获取Class类型的value
 *
 * @param keyPath 待获取的值的keyPath
 * @return 返回keyPath所指的类型的value(若keyPath所指的value不是对应的Class类型,则返回nil)
 */
- (nullable id)bba_valueForKeyPath:(NSString *)keyPath withValueClass:(Class)valueClass BDP_FOUNDATION_DEPRECATED("please use bdp_safeValueForKeyPath:withValueClass:");
- (nullable id)bdp_safeValueForKeyPath:(NSString *)keyPath withValueClass:(Class)valueClass;

@end

NS_ASSUME_NONNULL_END