如何在可可中将 NSIndexset 中的索引获取到 NSArray 中?

发布于 2024-09-24 09:19:44 字数 143 浏览 7 评论 0原文

我从表视图中获取选择的项目:

NSIndexSet *selectedItems = [aTableView selectedRowIndexes];

获取 NSArray 对象中的索引的最佳方法是什么?

I'm getting the select items from a table view with:

NSIndexSet *selectedItems = [aTableView selectedRowIndexes];

what's the best way to get the indexes in a NSArray object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

回心转意 2024-10-01 09:19:44

枚举集合,从索引中生成 NSNumbers,将 NSNumbers 添加到数组中。

你就是这么做的。不过,我不确定我是否明白将一组索引转换为效率较低的表示形式的意义。

要枚举一个集合,您有两种选择。如果您的目标操作系统是 OS X 10.6 或 iOS 4,则可以使用 enumerateIndexesUsingBlock:。如果您的目标是早期版本,则必须获取 firstIndex,然后继续对之前的结果请求 indexGreaterThanIndex:,直到获得 NSNotFound.

Enumerate the set, make NSNumbers out of the indexes, add the NSNumbers to an array.

That's how you'd do it. I'm not sure I see the point in transforming a set of indexes into a less efficient representation, though.

To enumerate a set, you have two options. If you're targeting OS X 10.6 or iOS 4, you can use enumerateIndexesUsingBlock:. If you're targeting earlier versions, you'll have to get the firstIndex and then keep asking for indexGreaterThanIndex: on the previous result until you get NSNotFound.

つ低調成傷 2024-10-01 09:19:44
NSIndexSet *selectedItems = [aTableView selectedRowIndexes];

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

NSMutableArray *selectedItemsArray=[NSMutableArray array];
    [selectedItems enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
        [selectedItemsArray addObject:[NSNumber numberWithInteger:idx]];
    }];
攀登最高峰 2024-10-01 09:19:44

快速地你可以做以下

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

然后你可以做

selectedItems.toArray()

With swift you can do the following

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

then you can do

selectedItems.toArray()
左耳近心 2024-10-01 09:19:44

这是示例代码:

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

NSArray *theObjects = [theItems objectsAtIndexes:filteredObjects]

可用性
适用于 iOS 2.0 及更高版本。

Here is the sample code:

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

NSArray *theObjects = [theItems objectsAtIndexes:filteredObjects]

Availability
Available in iOS 2.0 and later.

喵星人汪星人 2024-10-01 09:19:44

我通过在 NSIndexSet 上创建一个类别来做到这一点。这使得它变得小而高效,我只需要很少的代码。

我的接口(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;

/**
 * Returns an array of NSNumber objects representing each index in the set.
 */
- (NSArray<NSNumber*>*) arrayOfNumbersRepresentation;

/**
 *  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

和实现(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];
}

/**
 * Returns an array of NSNumber objects representing each index in the set.
 */
- (NSArray<NSNumber*>*) arrayOfNumbersRepresentation {
    NSMutableArray<NSNumber*> *result = [NSMutableArray array];

    [self enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
        [result addObject:[NSNumber numberWithInteger:idx]];
    }];

    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

I did it by creating a category on NSIndexSet. This kept it small and efficient, requiring very little code on my part.

My interface (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;

/**
 * Returns an array of NSNumber objects representing each index in the set.
 */
- (NSArray<NSNumber*>*) arrayOfNumbersRepresentation;

/**
 *  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

and the implementation (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];
}

/**
 * Returns an array of NSNumber objects representing each index in the set.
 */
- (NSArray<NSNumber*>*) arrayOfNumbersRepresentation {
    NSMutableArray<NSNumber*> *result = [NSMutableArray array];

    [self enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
        [result addObject:[NSNumber numberWithInteger:idx]];
    }];

    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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文