NSArray+RQExtension.m 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //
  2. // NSArray+RQExtension.m
  3. // XinShouJiaDao
  4. //
  5. // Created by 张嵘 on 2021/7/7.
  6. // Copyright © 2021 JCZ. All rights reserved.
  7. //
  8. #import "NSArray+RQExtension.h"
  9. @implementation NSArray (RQExtension)
  10. - (id)objAtPath:(NSIndexPath*)path {
  11. if (path.section < self.count ) {
  12. NSArray* subObj = self[path.section];
  13. if([subObj isKindOfClass:[NSArray class]] ||
  14. [subObj isKindOfClass:[NSMutableArray class]])
  15. {
  16. if (path.row < subObj.count) {
  17. return [self[path.section] objectAtIndex:path.row];
  18. }
  19. }
  20. }
  21. return nil;
  22. }
  23. /*
  24. * @brief 将数组随机打乱
  25. */
  26. - (NSArray *)rq_randomArray {
  27. // 转为可变数组
  28. NSMutableArray * tmp = self.mutableCopy;
  29. // 获取数组长度
  30. NSInteger count = tmp.count;
  31. // 开始循环
  32. while (count > 0) {
  33. // 获取随机角标
  34. NSInteger index = arc4random_uniform((int)(count - 1));
  35. // 获取角标对应的值
  36. id value = tmp[index];
  37. // 交换数组元素位置
  38. tmp[index] = tmp[count - 1];
  39. tmp[count - 1] = value;
  40. count --;
  41. }
  42. // 返回打乱顺序之后的数组
  43. return tmp.copy;
  44. }
  45. @end