Random Sequence

A category on NSIndexPath which provides serialisation to and initialisation from an NSArray.

Sponsored Links:

NSIndexPath + NSArray

How to Use:

 NSIndexPath *indexPath = [[[NSIndexPath indexPathWithIndex:1] indexPathByAddingIndex:2] indexPathByAddingIndex:3];
 NSArray *indexes = [indexPath allIndexes];
 NSIndexPath *anotherIndexPath = [NSIndexPath indexPathWithIndexes:indexes];

NSIndexPath+NSArray.h:

//
//  NSIndexPath+NSArray.h
//
//  Created by Johnnie Walker on 29/10/2008.
//
//  Public Domain

#import <Foundation/Foundation.h>

/*

 NSIndexPath *idp = [[[NSIndexPath indexPathWithIndex:1] indexPathByAddingIndex:2] indexPathByAddingIndex:3];
 NSLog(@"idp: %@",idp);
 NSLog(@"[idp allIndexes]: %@",[idp allIndexes]);   
 NSLog(@"[NSIndexPath indexPathWithIndexes:[idp allIndexes]]: %@",[NSIndexPath indexPathWithIndexes:[idp allIndexes]]);
 NSAssert([idp isEqual:[NSIndexPath indexPathWithIndexes:[idp allIndexes]]],@"Indexes don't match");

 */

@interface NSIndexPath (NSArray)
+ (NSIndexPath *)indexPathWithIndexes:(NSArray *)indexes;
- (NSIndexPath *)initWithIndexes:(NSArray *)indexes;
- (NSArray *)allIndexes;
@end

NSIndexPath+NSArray.m

//
//  NSIndexPath+NSArray.m
//
//  Created by Johnnie Walker on 29/10/2008.
//  Public Domain
//

#import "NSIndexPath+NSArray.h"

@implementation NSIndexPath (Array)
+ (NSIndexPath *)indexPathWithIndexes:(NSArray *)indexes
{
    return [[[NSIndexPath alloc] initWithIndexes:indexes] autorelease];
}

- (NSIndexPath *)initWithIndexes:(NSArray *)indexes
{
    NSUInteger idx[[indexes count]];
    for (NSInteger i=0; i<[indexes count]; i++) {
        idx[i] = [[indexes objectAtIndex:i] integerValue];      
    }

    return [self initWithIndexes:idx length:[indexes count]];
}

- (NSArray *)allIndexes
{
    NSMutableArray *indexes = [NSMutableArray arrayWithCapacity:[self length]];
    for (NSInteger i=0; i<[self length]; i++) {
        [indexes addObject:[NSNumber numberWithInteger:[self indexAtPosition:i]]];
    }
    return [[indexes copy] autorelease];
}
@end

Download

NSIndexPath-NSArray.zip (2 KB)

Sponsored Links: