NSArray+Safe.m 900 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //
  2. // NSArray+Safe.m
  3. // RuiZhi
  4. //
  5. // Created by RD on 2020/6/16.
  6. // Copyright © 2020 RDIOT. All rights reserved.
  7. //
  8. #import "NSArray+Safe.h"
  9. @implementation NSArray (Safe)
  10. - (id)objectAtIndexSafe:(NSUInteger)index{
  11. if (index < self.count) {
  12. return [self objectAtIndex:index];
  13. }else{
  14. NSLog(@"%s 数组越界~~", __func__);
  15. return nil;
  16. }
  17. }
  18. @end
  19. @implementation NSMutableArray (Safe)
  20. - (id)objectAtIndexSafe:(NSUInteger)index{
  21. if (index < self.count) {
  22. return [self objectAtIndex:index];
  23. }else{
  24. NSLog(@"%@ 数组越界~~", [self class]);
  25. return nil;
  26. }
  27. }
  28. - (void)addObjectSafe:(id)anObject{
  29. if (anObject != nil && [anObject isKindOfClass:[NSNull class]] == NO) {
  30. [self addObject:anObject];
  31. } else {
  32. NSLog(@"%@ 数组插入nil~~", [NSThread callStackSymbols]);
  33. }
  34. }
  35. @end