在 Swift 中,获取数组中最后两项的最简洁方法是什么? [英] In Swift, what's the cleanest way to get the last two items in an Array?

查看:49
本文介绍了在 Swift 中,获取数组中最后两项的最简洁方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有更简洁的方法来获取 Swift 中数组的最后两项?一般来说,我尽量避免这种方法,因为它很容易与索引一一对应.(此示例使用 Swift 1.2.)

//Swift -- 切片有点麻烦?让 oneArray = ["uno"]让 twoArray = ["uno", "dos"]让threeArray = [uno",dos",tres"]func getLastTwo(array: [String]) ->[细绳] {如果 array.count <= 1 {返回数组} 别的 {让切片:ArraySlice= array[array.endIndex-2..= 数组(切片)返回最后两个}}getLastTwo(oneArray)//["uno"]getLastTwo(twoArray)//["uno", "dos"]getLastTwo(threeArray)//["dos", "tres"]

我希望有一些更接近 Python 便利的东西.

## Python -- 非常方便的切片myList = ["uno", "dos", "tres"]打印 myList[-2:] # ["dos", "tres"]

解决方案

使用 Swift 5,根据您的需要,您可以选择以下模式之一,以便从数组的最后两个元素中获取新数组.

<小时>

#1.使用 Array 的 suffix(_:)

在 Swift 中,符合 Collection 协议的对象有一个 suffix(_:) 方法.Array 的 suffix(_:) 有以下声明:

func suffix(_ maxLength: Int) ->ArraySlice<元素>

<块引用>

返回一个子序列,直到给定的最大长度,包含集合的最终元素.

用法:

让数组 = [1, 2, 3, 4]让 arraySlice = array.suffix(2)让 newArray = Array(arraySlice)print(newArray)//打印:[3, 4]

<小时>

#2.使用 Arraysubscript(_:)

作为 suffix(_:) 方法的替代方法,您可以使用 Arraysubscript(_:) 下标:

让数组 = [1, 2, 3, 4]让范围 = array.index(array.endIndex, offsetBy: -2) ..<数组.endIndex//let range = array.index(array.endIndex, offsetBy: -2)...//也有效让 arraySlice = 数组 [范围]让 newArray = Array(arraySlice)print(newArray)//打印:[3, 4]

Is there a cleaner way to get the last two items of an array in Swift? In general, I try to avoid this approach since it's so easy to be off-by-one with the indexes. (Using Swift 1.2 for this example.)

// Swift -- slices are kind of a hassle?
let oneArray = ["uno"]
let twoArray = ["uno", "dos"]
let threeArray = ["uno", "dos", "tres"]

func getLastTwo(array: [String]) -> [String] {
    if array.count <= 1 {
        return array
    } else {
        let slice: ArraySlice<String> = array[array.endIndex-2..<array.endIndex]
        var lastTwo: Array<String> = Array(slice)
        return lastTwo
    }
}

getLastTwo(oneArray)   // ["uno"]
getLastTwo(twoArray)   // ["uno", "dos"]
getLastTwo(threeArray) // ["dos", "tres"]

I was hoping for something closer to Python's convenience.

## Python -- very convenient slices
myList = ["uno", "dos", "tres"]
print myList[-2:] # ["dos", "tres"]

解决方案

With Swift 5, according to your needs, you may choose one of the following patterns in order to get a new array from the last two elements of an array.


#1. Using Array's suffix(_:)

With Swift, objects that conform to Collection protocol have a suffix(_:) method. Array's suffix(_:) has the following declaration:

func suffix(_ maxLength: Int) -> ArraySlice<Element>

Returns a subsequence, up to the given maximum length, containing the final elements of the collection.

Usage:

let array = [1, 2, 3, 4]
let arraySlice = array.suffix(2)
let newArray = Array(arraySlice)
print(newArray) // prints: [3, 4]


#2. Using Array's subscript(_:)

As an alternative to suffix(_:) method, you may use Array's subscript(_:) subscript:

let array = [1, 2, 3, 4]
let range = array.index(array.endIndex, offsetBy: -2) ..< array.endIndex
//let range = array.index(array.endIndex, offsetBy: -2)... // also works
let arraySlice = array[range]
let newArray = Array(arraySlice)
print(newArray) // prints: [3, 4]

这篇关于在 Swift 中,获取数组中最后两项的最简洁方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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