如何在可可中将 NSIndexset 中的索引获取到 NSArray 中?
我从表视图中获取选择的项目:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
枚举集合,从索引中生成 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 thefirstIndex
and then keep asking forindexGreaterThanIndex:
on the previous result until you getNSNotFound
.快速地你可以做以下
然后你可以做
With swift you can do the following
then you can do
这是示例代码:
可用性
适用于 iOS 2.0 及更高版本。
Here is the sample code:
Availability
Available in iOS 2.0 and later.
我通过在 NSIndexSet 上创建一个类别来做到这一点。这使得它变得小而高效,我只需要很少的代码。
我的接口(NSIndexSet_Arrays.h):
和实现(NSIndexSet_Arrays.m):
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):
and the implementation (NSIndexSet_Arrays.m):