如何在数组中的每3个对象之后添加数组中的对象?

时间:2018-09-08 08:49:09

标签: ios arrays swift

我有一个从0到100的数组。

let array = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"...."100"]

现在,我想在数组中的每3个对象之后添加“-”。

所以输出将是这样

array = ["0", "1", "2","-","3", "4", "5","-", "6", "7", "8", "-","9"...."100"]

那么,我该如何实现呢?

如果我有模型数组而不是字符串数组怎么办?

   struct SubCategory { 
        var title: String = ""
        var subTitle: String = "" 
  }

  let array = [SubCategory]() 

2 个答案:

答案 0 :(得分:5)

let array = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]

extension Array {
    func chunks(of size: Int) -> [[Element]] {
        return stride(from: 0, to: count, by: size).map {
            let n = Swift.min(size, count - $0)
            return Array(self[$0 ..< $0 + n])
        }
    }
}

let joined = Array(array.chunks(of: 3).joined(separator: ["-"]))

答案 1 :(得分:2)

这里是一种使用单个map和一点数学就可以直接创建结果的方法:

extension Array {
    func adding(_ element: Element, afterEvery n: Int) -> [Element] {
        guard n > 0 else { fatalError("afterEvery value must be greater than 0") }
        let newcount = self.count + self.count / n
        return (0 ..< newcount).map { (i: Int) -> Element in
            (i + 1) % (n + 1) == 0 ? element : self[i - i / (n + 1)]
        }
    }
}

示例:

let result = ["0", "1", "2", "3", "4", "5", "6"].adding("-", afterEvery: 3)
print(result)

输出:

["0", "1", "2", "-", "3", "4", "5", "-", "6"]

示例2:

这次是[Int]

let result2 = [1, 2, 3, 4, 5, 6].adding(0, afterEvery: 2)
print(result2)

输出:

[1, 2, 0, 3, 4, 0, 5, 6, 0]

示例3:

使用自定义struct

struct SubCategory: CustomStringConvertible {
    var title = ""
    var subTitle = ""

    var description: String { return "SubCategory(title: \(title), subTitle: \(subTitle)" }
}

let array: [SubCategory] = [
    SubCategory(title: "2001", subTitle: "A Space Odyssey"),
    SubCategory(title: "Star Wars Episode 1", subTitle: "The Phantom Menace"),
    SubCategory(title: "Star Wars Episode 2", subTitle: "Attack of the Clones"),
    SubCategory(title: "Star Wars Episode 3", subTitle: "Revenge of the Sith"),
    SubCategory(title: "Star Wars Episode 4", subTitle: "A New Hope"),
    SubCategory(title: "Star Wars Episode 5", subTitle: "The Empire Strikes Back"),
    SubCategory(title: "Star Wars Episode 6", subTitle: "Return of the Jedi")
]

let result3 = array.adding(SubCategory(title: "none", subTitle: "none"), afterEvery: 3)
print(result3)

输出:

  

[SubCategory(title:2001,subTitle:A Space Odyssey,SubCategory(title:Star Wars Episode 1,subTitle:Phantom Menace,SubCategory(title:Star Wars Episode 2,subTitle:The Attack of the Clones,SubCategory(title: :无,subTitle:无,SubCategory(标题:星球大战第3集,subTitle:西斯的复仇,SubCategory(标题:Star Wars第4集,subTitle:A New Hope,SubCategory(title:星球大战第5集,subTitle:The 《帝国反击》,SubCategory(标题:无,subTitle:无,SubCategory(标题:《星球大战》第6集,subTitle:《绝地归来》)


正在更改版本

这是一个改变原始数组而不是创建新数组的版本:

extension Array {
    mutating func add(_ element: Element, afterEvery n: Int) {
        guard n > 0 else { fatalError("afterEvery value must be greater than 0") }
        var index = (self.count / n) * n
        while index > 0 {
            self.insert(element, at: index)
            index -= n
        }
    }
}

示例:

var array = ["0", "1", "2", "3", "4", "5", "6"]
array.add("-", afterEvery: 3)
print(array)

输出:

["0", "1", "2", "-", "3", "4", "5", "-", "6"]