2010-09-22 14 views
12

Sto ricevendo gli elementi selezionati da una vista tabella con:Come ottenere indici da NSIndexset in un NSArray nel cacao?

NSIndexSet *selectedItems = [aTableView selectedRowIndexes]; 

qual è il modo migliore per ottenere gli indici in un oggetto NSArray?

+1

Perché vuoi farlo? – Chuck

+0

Ho un metodo per ottenere un NSArray come parametro per eliminare più record selezionati dal tableview –

+6

Sembra un po 'circolare. Se lo scopo dell'array è quello di contenere un insieme di indici, perché il metodo non usa un NSIndexSet? – Chuck

risposta

20

Enumerare il set, eliminare NSNumbers dagli indici, aggiungere NSNumbers a un array.

Ecco come lo faresti. Non sono sicuro di vedere il punto nel trasformare un insieme di indici in una rappresentazione meno efficiente, però.

Per enumerare un set, sono disponibili due opzioni. Se scegli come target OS X 10.6 o iOS 4, puoi utilizzare enumerateIndexesUsingBlock:. Se hai scelto come target versioni precedenti, devi ottenere lo firstIndex e continuare a chiedere indexGreaterThanIndex: sul risultato precedente finché non ottieni NSNotFound.

+0

hai ragione è solo che non so come ottenere gli indici da NSIndexSet con un ciclo while o un ciclo foreach .... –

+5

È nei documenti: http://developer.apple.com/library /mac/#documentation/Cocoa/Conceptual/Collections/Articles/index.html%23//apple_ref/doc/uid/TP40010165-SW4 – Wevah

+0

@Wevah uomo perfetto !!! –

11
NSIndexSet *selectedItems = [aTableView selectedRowIndexes]; 

NSMutableArray *selectedItemsArray=[NSMutableArray array]; 
    [selectedItems enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) { 
     [selectedItemsArray addObject:[NSNumber numberWithInteger:idx]]; 
    }]; 
3

Con rapida è possibile effettuare le seguenti operazioni

extension NSIndexSet { 
    func toArray() -> [Int] { 
     var indexes:[Int] = []; 
     self.enumerateIndexesUsingBlock { (index:Int, _) in 
      indexes.append(index); 
     } 
     return indexes; 
    } 
} 

allora si può fare

selectedItems.toArray() 
+0

Potresti cancellare o aggiornare questa risposta? C'è un modo molto migliore in Swift: http://stackoverflow.com/a/37978236/3141234 – Alexander

1

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 
0

Ecco il codice di esempio:

NSIndexSet *filteredObjects = [items indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {do testing here}]; 

NSArray *theObjects = [theItems objectsAtIndexes:filteredObjects] 

Disponibilità Disponibile in iOS 2.0 e versioni successive.