微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从数组的末尾切片NSArray

如何解决从数组的末尾切片NSArray

| 从数组的末尾(而不是从开头)对“ 0”进行“切片”的最佳方法是什么(例如,找到包含长度未知的“ 0”的最后几个元素的子数组)?在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来源肯定会回报您的学习。执行切片操作时,列表对象将创建切片对象。切片对象的相关方法为
PySlice_GetIndicesEx
。您只需要小心地将这些索引变成
NSRange
。正如该函数中的注释警告说:“这比您想象的要难。” (稍后,我将尝试破解)。 更新:这里我们在
NSArray
上有一个切片类别。索引计算逻辑与我上面链接的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许可证的链接,并且还要注意,该链接可能仍然是“版权所有©2001-2010 Python Software Foundation;版权所有;保留所有权利”,因为尽管在我看来,这就像是可单独复制的衍生作品,但我不是律师。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。