ios 如何在迅捷中使用阵列切割?

oxalkeyp  于 2022-11-26  发布在  iOS
关注(0)|答案(8)|浏览(134)
var mentions = ["@alex", "@jason", "@jessica", "@john"]

我想将数组限制为3个项目,所以我想拼接它:

var slice = [String]()
if mentions.count > 3 {
    slice = mentions[0...3] //alex, jason, jessica
} else {
    slice = mentions
}

然而,我得到:
基类型为“[String]”且索引类型为“Range”的下标不明确
苹果雨燕2.2版(swiftlang-703.0.18.8 clang-703.0.31)目标用户:x86_64-苹果-macosx10.9

ui7jx7zq

ui7jx7zq1#

问题是mentions[0...3]返回的是ArraySlice<String>,而不是Array<String>。因此,您可以先使用Array(_:)初始化程序将切片转换为数组:

let first3Elements : [String] // An Array of up to the first 3 elements.
if mentions.count >= 3 {
    first3Elements = Array(mentions[0 ..< 3])
} else {
    first3Elements = mentions
}

或者如果你想使用一个ArraySlice(它们对于中间计算很有用,因为它们提供了一个原始数组的"视图",但不是为长期存储而设计的),你可以用else中的所有索引为mentions加下标:

let slice : ArraySlice<String> // An ArraySlice of up to the first 3 elements
if mentions.count >= 3 {
    slice = mentions[0 ..< 3]
} else {
    slice = mentions[mentions.indices] // in Swift 4: slice = mentions[...]
}

虽然到目前为止最简单的解决方案是使用prefix(_:)方法,该方法将返回前n个元素的ArraySlice,如果n超过数组计数,则返回整个数组的切片:

let slice = mentions.prefix(3) // ArraySlice of up to the first 3 elements
ljo96ir5

ljo96ir52#

我们可以这样做,

let arr = [10,20,30,40,50]
let slicedArray = arr[1...3]

如果要将分片数组转换为普通数组,

let arrayOfInts = Array(slicedArray)
0h4hbjxa

0h4hbjxa3#

您可以试试。prefix()。传回子序列,最大长度为指定的最大长度,包含集合的起始元素。如果最大长度超过集合中的元素数目,则结果会包含集合中的所有元素。

let numbers = [1, 2, 3, 4, 5]
print(numbers.prefix(2))    // Prints "[1, 2]"
print(numbers.prefix(10))   // Prints "[1, 2, 3, 4, 5]"
rkttyhzu

rkttyhzu4#

通用解决方案:

extension Array {
   func slice(size: Int) -> [[Element]] {
      (0...(count / size)).map{Array(self[($0 * size)..<(Swift.min($0 * size + size, count))])}
   }
}
ac1kyiln

ac1kyiln5#

还可以看看dropLast()函数:

var mentions:[String] = ["@alex", "@jason", "@jessica", "@john"]
var slice:[String] = mentions    
if mentions.count > 3 {
   slice = Array(mentions.dropLast(mentions.count - 3))
}
//print(slice) => ["@alex", "@jason", "@jessica"]
8wtpewkr

8wtpewkr6#

我想到了这个:

public extension Array {
    func slice(count: Int) -> [some Collection] {
        let n = self.count / count  // quotient
        let i = n * count           // index
        let r = self.count % count  // remainder
        let slices = (0..<n).map { $0 * count }.map { self[$0 ..< $0 + count] }
        return (r > 0) ? slices + [self[i..<i + r]] : slices
    }
}
klr1opcd

klr1opcd7#

也可以像这样进行切片:

//Generic Method
func slice<T>(arrayList:[T], limit:Int) -> [T]{
    return Array(arrayList[..<limit])
}

//How to Use
let firstThreeElements = slice(arrayList: ["@alex", "@jason", "@jessica", "@john"], limit: 3)
ryevplcw

ryevplcw8#

数组切片函数扩展:

extension Array {

    func slice(with sliceSize: Int) -> [[Element]] {

        guard self.count > 0 else { return [] }

        var range = self.count / sliceSize

        if self.count.isMultiple(of: sliceSize) {
            range -= 1
        }

        return (0...range).map { Array(self[($0 * sliceSize)..<(Swift.min(($0 + 1) * sliceSize, self.count))]) }
    }
}

相关问题