NSArray+NBAdditions.m 951 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. //
  2. // NSArray+NBAdditions.m
  3. // libPhoneNumber
  4. //
  5. // Created by Dave MacLachlan on 2/7/17.
  6. // Copyright © 2017 ohtalk.me. All rights reserved.
  7. //
  8. #import "NSArray+NBAdditions.h"
  9. @implementation NSArray (NBAdditions)
  10. - (id)nb_safeObjectAtIndex:(NSUInteger)index class:(Class)clazz {
  11. if (index >= self.count) {
  12. return nil;
  13. }
  14. id res = [self objectAtIndex:index];
  15. if (![res isKindOfClass:clazz]) {
  16. return nil;
  17. }
  18. return res;
  19. }
  20. - (NSString *)nb_safeStringAtIndex : (NSUInteger)index {
  21. return [self nb_safeObjectAtIndex:index class:[NSString class]];
  22. }
  23. - (NSNumber *)nb_safeNumberAtIndex:(NSUInteger)index {
  24. return [self nb_safeObjectAtIndex:index class:[NSNumber class]];
  25. }
  26. - (NSArray *)nb_safeArrayAtIndex:(NSUInteger)index {
  27. return [self nb_safeObjectAtIndex:index class:[NSArray class]];
  28. }
  29. - (NSData *)nb_safeDataAtIndex:(NSUInteger)index {
  30. return [self nb_safeObjectAtIndex:index class:[NSData class]];
  31. }
  32. @end