Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 25th, 2012  |  syntax: None  |  size: 0.85 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //
  2. //  NSArray+JTArraySplit.m
  3. //
  4. //  Created by james on 10/18/11.
  5. //
  6.  
  7. #import "NSArray+JTArraySplit.h"
  8.  
  9. @implementation NSArray (JTArraySplit)
  10.  
  11. + (NSArray *)splitArray:(NSArray *)targetArray componentsPerSegment:(NSUInteger)componentsCount {
  12.     NSMutableArray *splitedArray = [NSMutableArray array];
  13.  
  14.     NSUInteger targetArrayCount = [targetArray count];
  15.    
  16.     if (targetArrayCount > 0) {
  17.         int index = 0;
  18.         while (index < targetArrayCount) {
  19.             int length = MIN(targetArrayCount - index, componentsCount);
  20.             NSArray *subArray = [targetArray subarrayWithRange:NSMakeRange(index, length)];
  21.             [splitedArray addObject:subArray];
  22.             index = index+length;
  23.         }
  24.         return splitedArray;
  25.     } else {
  26.         // no objects inside targetArray, so just return empty array
  27.         return splitedArray;
  28.     }
  29. }
  30.  
  31. @end