NSMutableArray+Safety.h 3.06 KB
//
//  NSMutableArray+Safety.h
//  BBAFoundation
//
//  Created by Zhu,Yusong on 2018/9/25.
//  Copyright © 2018年 Baidu. All rights reserved.
//

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

/*
 * 可变数组的安全相关的扩展
 * 详细介绍请看README-SafeAPI
 */

NS_ASSUME_NONNULL_BEGIN

// 往数组中添加一个对象
#define ARRAY_ADD_OBJECT(array,object) \
do { \
[array bdp_safeAddObject:object]; \
} while(0)

@interface NSMutableArray (Safety)

/**
 * @brief 安全的添加对象,如果对象为nil,什么都不会发生
 *
 * @param anObject 添加的对象
 */
- (void)safe_addObject:(id)anObject BDP_FOUNDATION_DEPRECATED("please use bdp_safeAddObject:");
- (void)bdp_safeAddObject:(id)anObject;


/**
 * @brief 安全的从另一个数组里面添加对象,如果数组为nil,什么都不会发生
 *
 * @param otherArray 另一个数组
 */
- (void)safe_addObjectsFromArray:(NSArray *)otherArray BDP_FOUNDATION_DEPRECATED("please use bdp_safeAddObjectsFromArray:");
- (void)bdp_safeAddObjectsFromArray:(NSArray *)otherArray;

/**
 * @brief 安全的往数组index位置插入对象anObject. 无论anObject为nil或者index越界,都不会触发异常
 *
 * @param anObject id对象
 * @param index index
 */
- (void)safe_insertObject:(id)anObject atIndex:(NSUInteger)index BDP_FOUNDATION_DEPRECATED("please use bdp_safeInsertObject:atIndex:");
- (void)bdp_safeInsertObject:(id)anObject atIndex:(NSUInteger)index;

/**
 * @brief 安全的移除数组最后一个对象,确保数组为空也不会发生异常
 */
- (void)safe_removeLastObject BDP_FOUNDATION_DEPRECATED("please use bdp_safeRemoveLastObject:");
- (void)bdp_safeRemoveLastObject;

/**
 * @brief 安全的移除index位置的对象,即使index越界,也不会发生异常
 *
 * @param index index
 */
- (void)safe_removeObjectAtIndex:(NSUInteger)index BDP_FOUNDATION_DEPRECATED("please use bdp_safeRemoveObjectAtIndex:");
- (void)bdp_safeRemoveObjectAtIndex:(NSUInteger)index;

/**
 * @brief 移除对应范围内的所有对象的安全方法
 *
 * @param range 移除对象所在的范围range
 */
- (void)safe_removeObjectsInRange:(NSRange)range BDP_FOUNDATION_DEPRECATED("please use bdp_safeRemoveObjectsInRange:");
- (void)bdp_safeRemoveObjectsInRange:(NSRange)range;

/**
 * @brief 安全的替换数组index位置的对象,无论anObject为nil或者index越界,都不会触发异常
 *
 * @param index index
 * @param anObject replace Object
 */
- (void)safe_replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject BDP_FOUNDATION_DEPRECATED("please use bdp_safeReplaceObjectAtIndex:withObject:");
- (void)bdp_safeReplaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;

/**
 * @brief 安全的交换两个index位置的对象。
 *
 * @param idx1 idx1
 * @param idx2 idx2
 */
- (void)safe_exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2  BDP_FOUNDATION_DEPRECATED("please use bdp_safeExchangeObjectAtIndex:withObjectAtIndex:");
- (void)bdp_safeExchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2;

@end

NS_ASSUME_NONNULL_END