String 매서드, 프로퍼티
removeAll
Apple Developer Documentation
Apple Developer Documentation
var phrase = "The rain in Spain stays mainly in the plain."
let vowels: Set<Character> = ["a", "e", "i", "o", "u"]
phrase.removeAll(where: { vowels.contains($0) })
enum HTTPResponse {
case ok
case error(Int)
}
let lastThreeResponses: [HTTPResponse] = [.ok, .ok, .error(404)]
let hadError = lastThreeResponses.contains { element in
if case .error = element {
return true
} else {
return false
}
}
filter
: 특정 조건 만족하는 것만 뽑는 것
Apple Developer Documentation
let cast = ["Vivien", "Marlon", "Kim", "Karl"]
let shortNames = cast.filter { $0.count < 5 }
print(shortNames)
popLast
Apple Developer Documentation
firstIndex
Apple Developer Documentation
var students = ["Ben", "Ivy", "Jordell", "Maxime"]
if let i = students.firstIndex(of: "Maxime") {
students[i] = "Max"
}
print(students)
split
Apple Developer Documentation
let line = "BLANCHE: I don't want realism. I want magic!"
print(line.split(whereSeparator: { $0 == " " }))
print(line.split(maxSplits: 1, whereSeparator: { $0 == " " }))
print(line.split(omittingEmptySubsequences: false, whereSeparator: { $0 == " " }))
reduce
Apple Developer Documentation
let numbers = [1, 2, 3, 4]
let numberSum = numbers.reduce(0, { x, y in
x + y
})
map
: 블락 안의 조건 만족도록 뽑는 것
Apple Developer Documentation
let cast = ["Vivien", "Marlon", "Kim", "Karl"]
let lowercaseNames = cast.map { $0.lowercased() }
let letterCounts = cast.map { $0.count }
forEach()
Apple Developer Documentation
let numberWords = ["one", "two", "three"]
for word in numberWords {
print(word)
}
numberWords.forEach { word in
print(word)
}
enumberated()
Apple Developer Documentation
for (n, c) in "Swift".enumerated() {
print("\(n): '\(c)'")
}
sorted()
Apple Developer Documentation
let students: Set = ["Kofi", "Abena", "Peter", "Kweku", "Akosua"]
let descendingStudents = students.sorted(by: >)
print(descendingStudents)
index
Apple Developer Documentation
Apple Developer Documentation
Apple Developer Documentation
Apple Developer Documentation
var c = MyFancyCollection([10, 20, 30, 40, 50])
var i = c.startIndex
while i != c.endIndex {
c[i] /= 5
i = c.index(after: i)
}