tôi đã làm nó bằng cách tạo ra một thể loại trên NSIndexSet. Điều này giữ nó nhỏ và hiệu quả, đòi hỏi rất ít mã trên một phần của tôi.
giao diện của tôi (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
và thực hiện (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
Tại sao bạn muốn thực hiện việc này? – Chuck
tôi có một phương thức nhận được một NSArray làm tham số để xóa nhiều bản ghi được chọn từ tableview –
Điều đó có vẻ hơi tròn. Nếu mục đích của mảng là chứa một tập hợp các chỉ mục, tại sao không có phương thức thay thế một NSIndexSet? – Chuck