gpt4 book ai didi

objective-c - 从数组末尾切片 NSArray

转载 作者:可可西里 更新时间:2023-11-01 03:26:49 25 4
gpt4 key购买 nike

从数组的末尾而不是开头“切片”NSArray 的最佳方法是什么(例如,找到包含 的最后几个元素的子数组未知长度的 NSArray)?在 Python 中,您可以使用负索引来完成此操作,例如:

new_list = old_list[-5:-3]

在 Objective-C 中最自然的方法是什么?

最佳答案

没有什么比得上 Python 的漂亮语法了,但你可以这样做:

NSUInteger count = [myArray count];
NSArray * slice = [myArray subarrayWithRange:(NSRange){count-n, n}];

您还可以为 NSArray 编写一个类别,例如:

@interface NSArray (jrdioko_slice)
- (NSArray *) jrdioko_sliceFrom:(NSInteger)start to:(NSInteger)stop;
@end

如果你想走这条路,Python源码肯定是值得学习的。 list object创建一个 slice object当执行切片操作时。切片对象的相关方法是 PySlice_GetIndicesEx。您只需要小心地将这些索引转换为 NSRange。正如该功能中的评论警告“这比你想象的更难做到正确”。 (我稍后会尝试解决这个问题。)

更新:这里我们在 NSArray 上有一个切片类别。索引计算逻辑几乎直接来 self 上面链接的 Python 代码。*如果您不必担心 Python 切片的步幅部分,它实际上比我最初想象的要容易得多。我已经通过一些测试运行它,它似乎与 Python 版本一样工作。

@interface NSArray (WSS_Slice)
- (NSArray *)WSS_arrayBySlicingFrom:(NSInteger)start to:(NSInteger)stop;
@end

// Python allows skipping any of the indexes of a slice and supplies default
// values. Skipping an argument to a method is not possible, so (ab)use
// NSNotFound as "not specified" index value. The other way to do this would
// be with varargs, which might be even handier if one decided to implement
// the stride functionality.
enum {
WSS_SliceNoIndex = NSNotFound
};

@implementation NSArray (WSS_Slice)

- (NSArray *)WSS_arrayBySlicingFrom:(NSInteger)start to:(NSInteger)stop {
// There's an important caveat here: specifying the parameters as
// NSInteger allows negative indexes, but limits the method's
// (theoretical) use: the maximum size of an NSArray is NSUIntegerMax,
// which is quite a bit larger than NSIntegerMax.
NSUInteger count = [self count];

// Due to this caveat, bail if the array is too big.
if( count >= NSIntegerMax ) return nil;

// Define default start and stop
NSInteger defaultStart = 0;
NSInteger defaultStop = count;

// Set start to default if not specified
if( start == WSS_SliceNoIndex ){
start = defaultStart;
}
else {
// If start is negative, change it to the correct positive index.
if( start < 0 ) start += count;
// Correct for out-of-bounds index:
// If it's _still_ negative, set it to 0
if( start < 0 ) start = 0;
// If it's past the end, set it to just include the last item
if( start > count ) start = count;
}

// Perform all the same calculations on stop
if( stop == WSS_SliceNoIndex ){
stop = defaultStop;
}
else {
if( stop < 0 ) stop += count;
if( stop < 0 ) stop = 0;
if( stop > count ) stop = count;
}

// Calculate slice length with corrected indexes
NSInteger sliceLength = stop - start;

// If no slice, return a new empty array
if( sliceLength <= 0 ){
return [NSArray array];
}
else {
return [self subarrayWithRange:(NSRange){start, sliceLength}];
}

}
@end

*因此我想我需要包含一个指向 Python License 的链接还要注意,这可能仍然是“版权所有 © 2001-2010 Python Software Foundation;保留所有权利”,因为虽然这在我看来像是一个单独受版权保护的衍生作品,但我不是律师。

关于objective-c - 从数组末尾切片 NSArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6337339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com