Files
real-e-party-iOS/YuMi/Tools/Safe/NSMutableArray+Safe.m
edwinQQQ a35a711be6 chore: Initial clean commit
- Removed YuMi/Library/ (138 MB, not tracked)
- Removed YuMi/Resources/ (23 MB, not tracked)
- Removed old version assets (566 files, not tracked)
- Excluded Pods/, xcuserdata/ and other build artifacts
- Clean repository optimized for company server deployment
2025-10-09 16:19:14 +08:00

69 lines
1.9 KiB
Objective-C

//
// NSMutableArray+Safe.m
// YUMI
//
// Created by YUMI on 2023/11/15.
//
#import "NSMutableArray+Safe.h"
@implementation NSMutableArray (Safe)
- (void)xpSafeRemoveObjectAtIndex:(NSUInteger)index {
if ([self isKindOfClass:[NSMutableArray class]]) {
if (self.count > 0 && index < self.count) {
[self removeObjectAtIndex:index];
}
}
}
- (void)xpSafeRemoveObject:(id)anObject {
if ([self isKindOfClass:[NSMutableArray class]]) {
if (anObject && [self containsObject:anObject]) {
[self removeObject:anObject];
}
}
}
- (void)xpSafeRemoveObjectsAtIndexes:(NSIndexSet *)indexes {
if ([self isKindOfClass:[NSMutableArray class]] && indexes) {
// 创建一个有效索引的集合
NSMutableIndexSet *validIndexes = [NSMutableIndexSet indexSet];
[indexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
if (idx < self.count) {
[validIndexes addIndex:idx];
}
}];
if (validIndexes.count > 0) {
[self removeObjectsAtIndexes:validIndexes];
}
}
}
- (void)xpSafeInsertObject:(id)anObject atIndex:(NSUInteger)index {
if ([self isKindOfClass:[NSMutableArray class]] && anObject) {
if (index <= self.count) {
[self insertObject:anObject atIndex:index];
} else if (index > self.count) {
// 如果索引超出范围,则添加到末尾
[self addObject:anObject];
}
}
}
- (void)xpSafeReplaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject {
if ([self isKindOfClass:[NSMutableArray class]] && anObject) {
if (index < self.count) {
[self replaceObjectAtIndex:index withObject:anObject];
}
}
}
- (void)xpSafeRemoveAllObjects {
if ([self isKindOfClass:[NSMutableArray class]] && self.count > 0) {
[self removeAllObjects];
}
}
@end