配列を整理するときに便利な関数をまとめました。
map
配列の要素を1つずつ変換する。
let cast = ["Vivien", "Marlon", "Kim", "Karl"]
let lowercaseNames = cast.map { $0.lowercased() }
// 'lowercaseNames' == ["vivien", "marlon", "kim", "karl"]
let letterCounts = cast.map { $0.count }
// 'letterCounts' == [6, 6, 3, 4]
https://developer.apple.com/documentation/swift/array/3017522-map より
compactMap
クロージャーでnilだったものを除外して処理する。
let possibleNumbers = ["1", "2", "three", "///4///", "5"]
let mapped: [Int?] = possibleNumbers.map { str in Int(str) }
// [1, 2, nil, nil, 5]
let compactMapped: [Int] = possibleNumbers.compactMap { str in Int(str) }
// [1, 2, 5]
https://developer.apple.com/documentation/swift/array/2957701-compactmap より
flatMap
配列の要素を1つずつ分解して配列に追加する。
let numbers = [1, 2, 3, 4]
let mapped = numbers.map { Array(repeating: $0, count: $0) }
// [[1], [2, 2], [3, 3, 3], [4, 4, 4, 4]]
let flatMapped = numbers.flatMap { Array(repeating: $0, count: $0) }
// [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
https://developer.apple.com/documentation/swift/sequence/2905332-flatmap より
reduce
配列の要素を1つの値にまとめる。
let numbers = [1, 2, 3, 4]
let numberSum = numbers.reduce(0, { x, y in
x + y
})
// numberSum == 10
https://developer.apple.com/documentation/swift/array/2298686-reduce より
allSatisfy
配列の全要素がクロージャーの条件を満たすか判別。
let names = ["Sofia", "Camilla", "Martina", "Mateo", "Nicolás"]
let allHaveAtLeastFive = names.allSatisfy({ $0.count >= 5 })
// allHaveAtLeastFive == true
https://developer.apple.com/documentation/swift/array/2994715-allsatisfy より
sort
配列を昇順あるいは降順に並び替える。
var students = ["Kofi", "Abena", "Peter", "Kweku", "Akosua"]
students.sort()
print(students)
// Prints "["Abena", "Akosua", "Kofi", "Kweku", "Peter"]"
students.sort(by: >)
print(students)
// Prints "["Peter", "Kweku", "Kofi", "Akosua", "Abena"]"
https://developer.apple.com/documentation/swift/array/1688499-sort より
filter
クロージャーの条件を満たす要素のみ抜き出す。
let cast = ["Vivien", "Marlon", "Kim", "Karl"]
let shortNames = cast.filter { $0.count < 5 }
print(shortNames)
// Prints "["Kim", "Karl"]"
https://developer.apple.com/documentation/swift/sequence/3018365-filter より
forEach
配列の中身を繰り返す。
let numberWords = ["one", "two", "three"]
for word in numberWords {
print(word)
}
// Prints "one"
// Prints "two"
// Prints "three"
numberWords.forEach { word in
print(word)
}
// Prints "one"
// Prints "two"
// Prints "three"
https://developer.apple.com/documentation/swift/array/1689783-foreach より
joined
要素の間に指定した区切り文字を追加した上で配列の中身を連結する。
let cast = ["Vivien", "Marlon", "Kim", "Karl"]
let list = cast.joined(separator: ", ")
print(list)
// Prints "Vivien, Marlon, Kim, Karl"
https://developer.apple.com/documentation/swift/sequence/1641243-joined より
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]"
https://developer.apple.com/documentation/swift/array/1689487-prefix より
suffix
配列の後ろからの範囲を指定して分割。
let numbers = [1, 2, 3, 4, 5]
print(numbers.suffix(2))
// Prints "[4, 5]"
print(numbers.suffix(10))
// Prints "[1, 2, 3, 4, 5]"
https://developer.apple.com/documentation/swift/array/1687909-suffix より
minElement
配列の最小要素を取得。
let scores = [84, 76, 91, 62 ,80]
scores.minElement() // 62
https://qiita.com/motokiee/items/cf83b22cb34921580a52 より
maxElement
配列の最大要素を取得。
let scores = [84, 76, 91, 62 ,80]
scores.maxElement() // 91
https://qiita.com/motokiee/items/cf83b22cb34921580a52 より
dropFirst
配列の最初から指定した個数分の要素を削除。
let numbers = [1, 2, 3, 4, 5]
print(numbers.dropFirst(2))
// Prints "[3, 4, 5]"
print(numbers.dropFirst(10))
// Prints "[]"
https://developer.apple.com/documentation/swift/array/1688675-dropfirst より
dropLast
配列の最後から指定した個数分の要素を削除。
let numbers = [1, 2, 3, 4, 5]
print(numbers.dropLast(2))
// Prints "[1, 2, 3]"
print(numbers.dropLast(10))
// Prints "[]"
https://developer.apple.com/documentation/swift/array/1689751-droplast より
reverse
配列の順番を逆にする。
var characters: [Character] = ["C", "a", "f", "é"]
characters.reverse()
print(characters)
// Prints "["é", "f", "a", "C"]
https://developer.apple.com/documentation/swift/array/2943836-reverse より
shuffle
配列の順番をランダムに並び替える。
var names = ["Alejandro", "Camila", "Diego", "Luciana", "Luis", "Sofía"]
names.shuffle(using: myGenerator)
// names == ["Luis", "Camila", "Luciana", "Sofía", "Alejandro", "Diego"]
https://developer.apple.com/documentation/swift/array/2994753-shuffle より
insert
配列の特定の位置に要素を追加する。
var numbers = [1, 2, 3, 4, 5]
numbers.insert(100, at: 3)
numbers.insert(200, at: numbers.endIndex)
print(numbers)
// Prints "[1, 2, 3, 100, 4, 5, 200]"
https://developer.apple.com/documentation/swift/array/3126951-insert より
Swiftのお役立ち情報