从数组末尾切片 NSArray
从数组末尾而不是开头“切片” NSArray 的最佳方法是什么(例如,查找包含 NSArray 最后几个元素的子数组)代码> 长度未知)?在 Python 中,您可以使用负索引来完成此操作,例如:
new_list = old_list[-5:-3]
在 Objective-C 中执行此操作最自然的方法是什么?
What is the best way to "slice" an NSArray
from the end, rather than the beginning, of the array (for example, finding the subarray containing the last few elements of a NSArray
of unknown length)? In Python, you can use negative indices to accomplish this, e.g.:
new_list = old_list[-5:-3]
What's the most natural way to do this in Objective-C?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有什么可以与 Python 的良好语法相匹配,但你可以这样做:
你还可以为
NSArray
编写一个类别,例如:如果你想走这条路,Python 源代码肯定会回报你的学习。 列表对象创建一个 切片对象时切片执行操作。切片对象的相关方法是
PySlice_GetIndicesEx
。您只需小心地将这些索引转换为NSRange
即可。正如该函数中的评论所警告的那样“这比您想象的更难做到正确”。 (稍后我会尝试解决这个问题。)更新:这里我们在
NSArray
上有一个切片类别。索引计算逻辑几乎直接来自我上面链接的 Python 代码。*如果您不必担心 Python 切片的步幅部分,它实际上比我一开始想象的要容易得多。我已经通过一些测试运行了它,它的工作原理似乎与 Python 版本相同。*因此我认为我需要包含一个指向 Python 许可证 的链接,并请注意, 可能仍然是“版权所有 © 2001-2010 Python Software Foundation;保留所有权利”,因为虽然这在我看来像是一个单独受版权保护的衍生作品,但我不是律师。
There's nothing to match Python's nice syntax for this, but you could do:
You could also write up a category for
NSArray
, something like:If you want to go this route, the Python source will certainly repay study. A list object creates a slice object when a slice operation is performed. The relevant method on a slice object is
PySlice_GetIndicesEx
. You'll just have to be careful turning those indexes into anNSRange
. As the comment in that function warns "this is harder to get right than you might think". (I'll try to take a crack at this later.)UPDATE: Here we have a slice category on
NSArray
. The index calculation logic is pretty much straight out of the Python code that I linked to above.* It's actually a lot easier than I thought at first if you don't have to worry about the stride part of a Python slice. I've run this through a few tests and it seems to work the same as the Python version.*Therefore I think I need to include a link to the Python License and also note that this may still be “Copyright © 2001-2010 Python Software Foundation; All Rights Reserved”, because although this looks to me like a separately-copyrightable derivative work, I ain't a lawyer.