将下一个或上一个项目获取到Swift集合或数组中的对象 [英] Get next or previous item to an object in a Swift collection or array

查看:137
本文介绍了将下一个或上一个项目获取到Swift集合或数组中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在防止超出范围的错误的同时,识别Swift数组中指定对象之前或之后的项的最佳方法是什么?

What is the best way to identify the item that precedes or follows a specified object in a Swift array, while protecting against out of bounds errors?

推荐答案

一种很好的解决方法是在Swift Array上进行扩展,或者在这种情况下,为所有BidirectionalCollection对象(包括数组)提供更通用的解决方案.

A good way to approach this is with an extension on the Swift Array, or in this case a more generalised solution for all BidirectionalCollection objects, including arrays.

以下提供了从数组中获取指定对象之后的下一个或上一个对象的方法,如果希望函数在数组的末尾循环,则使用可选参数.

The following provides methods for getting the next or previous object after your specified object from an array, with an optional parameter if you want the function to loop at the ends of the array.

如果原始对象不存在于Array中,并且对于非循环函数,如果您要求将第一个对象的上一个项目或最后一个对象之后的项目返回,则这些函数返回nil.

These functions return nil if the original object is not present in the Array, and also if you ask for the previous item to the first object or the item that follows the last object, for the non-looping functions.

//
//  Array+Iterator.swift
//

extension BidirectionalCollection where Iterator.Element: Equatable {
    typealias Element = Self.Iterator.Element

    func after(_ item: Element, loop: Bool = false) -> Element? {
        if let itemIndex = self.index(of: item) {
            let lastItem: Bool = (index(after:itemIndex) == endIndex)
            if loop && lastItem {
                return self.first
            } else if lastItem {
                return nil
            } else {
                return self[index(after:itemIndex)]
            }
        }
        return nil
    }

    func before(_ item: Element, loop: Bool = false) -> Element? {
        if let itemIndex = self.index(of: item) {
            let firstItem: Bool = (itemIndex == startIndex)
            if loop && firstItem {
                return self.last
            } else if firstItem {
                return nil
            } else {
                return self[index(before:itemIndex)]
            }
        }
        return nil
    }
}

用法: 如果您有很多孩子,并且想知道简之后的孩子,则可以使用以下代码:

Usage: If you have an array of your children, and want to know the child that comes after Jane, you would use the following:

let nextChild = children.after(jane)

如果您只是想知道轮到谁来洗碗,而Sammy昨晚做了,那么您应该使用:

If you simply want to know whose turn it is to do the dishes, and Sammy did them last night, you'd instead use:

let dishwasherTonight = children.after(sammy, loop: true)

这样,如果Sammy是最小的孩子,那么当我们循环回到数组的开头时,他最大的兄弟姐妹将被分配今晚洗碗.

That way, if Sammy is the youngest child, his oldest sibling will be assigned to wash the dishes tonight as we loop back to the start of the array.

后记:请注意在代码中该属性的定义中与endIndex的比较:

您可以通过下标通过其下标访问集合的元素 使用除集合的endIndex属性以外的任何有效索引.这 属性是一个"past the end"索引,与任何索引都不对应 集合的元素.

You can access an element of a collection through its subscript by using any valid index except the collection’s endIndex property. This property is a "past the end" index that does not correspond with any element of the collection.

这篇关于将下一个或上一个项目获取到Swift集合或数组中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆