Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在斯威夫特,什么';获取数组中最后两项的最干净方法是什么?_Arrays_Swift_Slice - Fatal编程技术网

Arrays 在斯威夫特,什么';获取数组中最后两项的最干净方法是什么?

Arrays 在斯威夫特,什么';获取数组中最后两项的最干净方法是什么?,arrays,swift,slice,Arrays,Swift,Slice,有没有更干净的方法可以在Swift中获取数组的最后两项?一般来说,我尽量避免使用这种方法,因为它很容易被索引关闭。(本例使用Swift 1.2。) 我怀疑这会让你更快乐,但数学肯定更简单: func getLastTwo(array: [String]) -> [String] { if array.count <= 1 { return array } else { return array.reverse()[0...1].reve

有没有更干净的方法可以在Swift中获取数组的最后两项?一般来说,我尽量避免使用这种方法,因为它很容易被索引关闭。(本例使用Swift 1.2。)


我怀疑这会让你更快乐,但数学肯定更简单:

func getLastTwo(array: [String]) -> [String] {
    if array.count <= 1 {
        return array
    } else {
        return array.reverse()[0...1].reverse()
    }
}
func getlastwo(数组:[String])->[String]{

如果array.count更一般的答案

let a1 = [1,2,3,4,5]
let a2 = ["1","2","3","4","5"]

func getLast<T>(array: [T], count: Int) -> [T] {
  if count >= array.count {
    return array
  }
  let first = array.count - count
  return Array(array[first..<first+count])
}

getLast(a1, count: 2) // [4, 5]
getLast(a2, count: 3) // ["3", "4", "5"]
设a1=[1,2,3,4,5]
设a2=[“1”、“2”、“3”、“4”、“5”]
func getLast(数组:[T],计数:Int)->[T]{
如果计数>=array.count{
返回数组
}
让第一个=array.count-count

返回数组(数组[first..在Swift 2中,您可以扩展
CollectionType
。下面是一个示例(借用Rob Napier的答案):

以下是
CharacterView

let string = "looking"
print(string.characters.last(4)) // [k, i, n, g]

(请注意,我的示例在所有情况下都返回数组,而不是原始集合类型。)

Swift中数组的最后两项

编辑:首先检查myArray.count>=2

let myArray2:Array? = myArray.count >= 2 ? [myArray[myArray.count-2], myArray[myArray.count-1]] : nil
在这里,它被包装在一个函数中,该函数接受数组并返回包含最后两项的数组,或者如果传递的数组不包含至少两项,则返回nil

func getLastTwo(myArray:[String]) -> [String]? {
        return myArray.count >= 2 ? [myArray[myArray.count-2], myArray[myArray.count-1]] : nil
    }
myList[-2:][/code>

是的,我有一个增强请求,要求使用负索引符号,我建议你也提交一个

但是,您不应该让这件事变得更难。内置的global
suffix
函数正是您想要的:

let oneArray = ["uno"]
let twoArray = ["uno", "dos"]
let threeArray = ["uno", "dos", "tres"]

let arr1 = suffix(oneArray,2) // ["uno"]
let arr2 = suffix(twoArray,2) // ["uno", "dos"]
let arr3 = suffix(threeArray,2) // ["dos", "tres"]

结果是一个切片,但如果需要,您可以将其强制为数组。

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


#1.使用数组的
后缀(:)
使用Swift,符合
集合
协议的对象具有
后缀(:)
方法。数组具有以下声明:

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

#2.使用
数组
下标(:)
作为
后缀(:)
方法的替代方法,您可以使用
数组的下标:

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]
let数组=[1,2,3,4]
让range=array.index(array.endIndex,offsetBy:-2)…
让项目=[0,2,5,3,7,6,9,10]
让计数=items.count
让last2=项目[count-2..
Swift4解决方案:

let oneArray = ["uno"]
let twoArray = ["uno", "dos"]
let threeArray = ["uno", "dos", "tres"]

let arr1 = threeArray.suffix(from: threeArray.count-2) // ["dos", "tres"]
阐明Swift内置函数
func后缀(起始:Int)->ArraySlice
功能的其他示例有

let arr2 = oneArray.suffix(from: 0) // ["uno"]
let arr3 = twoArray.suffix(from: 0) // ["uno", "dos"]
let arr4 = twoArray.suffix(from: 1) // ["dos"]
let arr5 = threeArray.suffix(from: 1) // ["dos", "tres"]
let arr6 = threeArray.suffix(from: 2) // ["tres"]

swift 5中,可以对最后一个对象使用后缀,对第一个对象使用前缀,下面是一个示例:

let exampleArray = ["first text", "second text", "third text"]

let arr1 = exampleArray.suffix(2) // ["second text", "third text"]
let arr2 = exampleArray.prefix(2) // ["first text", "second text"]

结果是一个切片,但如果需要,可以将其强制为数组。

“我的示例在所有情况下都返回一个数组”不过,这似乎非常合理;
map
的行为方式与此相同,IIRC。如果需要,您可以随时回溯到任何内容。感谢您的回答,但如果数组中不保证至少包含两个项,则这并不安全。已经有一个内置函数可以精确给出您指定的结果-
后缀()
。我已经添加了一个答案,显示了它的实际作用……这个问答和优秀的答案可能对任何在这里搜索的人都有用:我们怎么会错过这个这么久?@MartinR我们忘了读我的书。包括我。但我不会收回我对其他答案的任何赞扬,特别是因为它们暗示了
后缀
可能是什么实现。这是一个令人惊讶的教育主题。在Swift 2.0中,后缀(MyColarray,3)
变成了
MyColarray。后缀(3)
认为我会分享,所以没有人会像我那样拔出那么多头发。dropFirst:(长度-2)@Joe Blow这是2015年6月,伙计。我认为它会导致崩溃,因为没有项目[计数]的索引
let array = [1, 2, 3, 4]
let arraySlice = array.suffix(2)
let newArray = Array(arraySlice)
print(newArray) // prints: [3, 4]
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]
let items = [0, 2, 5, 3, 7, 6, 9, 10]
let count = items.count
let last2 = items[count - 2 ..< count] // [9, 10]
let oneArray = ["uno"]
let twoArray = ["uno", "dos"]
let threeArray = ["uno", "dos", "tres"]

let arr1 = threeArray.suffix(from: threeArray.count-2) // ["dos", "tres"]
let arr2 = oneArray.suffix(from: 0) // ["uno"]
let arr3 = twoArray.suffix(from: 0) // ["uno", "dos"]
let arr4 = twoArray.suffix(from: 1) // ["dos"]
let arr5 = threeArray.suffix(from: 1) // ["dos", "tres"]
let arr6 = threeArray.suffix(from: 2) // ["tres"]
let exampleArray = ["first text", "second text", "third text"]

let arr1 = exampleArray.suffix(2) // ["second text", "third text"]
let arr2 = exampleArray.prefix(2) // ["first text", "second text"]