l'ho fatto con la creazione di una categoria a NSIndexSet. Ciò lo ha tenuto piccolo ed efficiente, richiedendo pochissimo codice da parte mia.
mia interfaccia (NSIndexSet_Arrays.h):
/**
* Provides a category of NSIndexSet that allows the conversion to and from an NSDictionary
* object.
*/
@interface NSIndexSet (Arrays)
/**
* Returns an NSArray containing the contents of the NSIndexSet in a format that can be persisted.
*/
- (NSArray*) arrayRepresentation;
/**
* Initialises self with the indexes found wtihin the specified array that has previously been
* created by the method @see arrayRepresentation.
*/
+ (NSIndexSet*) indexSetWithArrayRepresentation:(NSArray*)array;
@end
e l'implementazione (NSIndexSet_Arrays.m):
#import "NSIndexSet_Arrays.h"
@implementation NSIndexSet (Arrays)
/**
* Returns an NSArray containing the contents of the NSIndexSet in a format that can be persisted.
*/
- (NSArray*) arrayRepresentation {
NSMutableArray *result = [NSMutableArray array];
[self enumerateRangesUsingBlock:^(NSRange range, BOOL *stop) {
[result addObject:NSStringFromRange(range)];
}];
return [NSArray arrayWithArray:result];
}
/**
* Initialises self with the indexes found wtihin the specified array that has previously been
* created by the method @see arrayRepresentation.
*/
+ (NSIndexSet*) indexSetWithArrayRepresentation:(NSArray*)array {
NSMutableIndexSet *result = [NSMutableIndexSet indexSet];
for (NSString *range in array) {
[result addIndexesInRange:NSRangeFromString(range)];
}
return result;
}
@end
Perché vuoi farlo? – Chuck
Ho un metodo per ottenere un NSArray come parametro per eliminare più record selezionati dal tableview –
Sembra un po 'circolare. Se lo scopo dell'array è quello di contenere un insieme di indici, perché il metodo non usa un NSIndexSet? – Chuck