let sum = [1, 2, 3, 4].reduce(0) { $0 + $1 } 0은 sum의 초기값을 말한다. reduce는 파라미터가 2개인데 0이 $0로 가고 배열의 첫번째 값이 $1로 간다. 그리고 그 두 수의 합은 다시 $0가 되고 배열의 두번째 값이 $1이 된다. 그렇게 끝까지 돌아가면 10이라는 결과가 나온다.
옵셔널 선언 시 ! 선언은 무엇인가?
let a: Int! = 234 // Optional<234>
let b: Int = 34
print(a + b)
// 268! 선언은 ? 선언과 달리 연산 시 자동적으로 옵셔널이 풀린다.
var arr = [[0, 3], [1, 9], [2, 6]]
print(arr[1][1]) // 9A slice of a string.
sort는 기본적으로 원본 배열을 정렬한다.
var arr = [2, 24, 45, 36, 9]
arr.sort()
print(arr) // [2, 9, 24, 36, 45]
arr.sort(by: >)
print(arr) // [45, 36, 24, 9, 2]sorted는 원본 배열은 건드리지 않고 사본을 만들어서 오름차순으로 정렬한 후 정렬된 요소를 반환해주는 방식이다. 내림차순 방식은 sort와 똑같다.
var arr = [2, 24, 45, 36, 9]
var sortedArr = arr.sorted()
print(arr) // 변함없음
print(sortedArr) // [2, 9, 24, 36, 45]let a: Int = 17
print(a / 5)
// 3var dict: [String: Int] = [:]
dict["d"] = 3
dict.updateValue(4, forKey: "f")
print(dict)
// ["f": 4, "d": 3]
var dict: [[Int]: [Int]] = [:]
dict.updateValue([3, 3], forKey: [3, 3])
print(dict)
//[[3, 3]: [3, 3]]let items = [1, 1, 1, 2, 2, 2, 2, 3, 3, 4]
let mappedItems = items.map { ($0, 1) }
let counts = Dictionary(mappedItems, uniquingKeysWith: +)
print(counts.max(by: { $0.value < $1.value })!.key)
// 2
let str = Dictionary(items.map { ($0, 1) }, uniquingKeysWith: +)
print(str.max(by: { $0.value < $1.value })!.key)
// 2var s = "Swift"
var i = s.index(s.startIndex, offsetBy: 2)
s = String(s[s.startIndex...i])
print(s) // Swihttps://greendreamtrre.tistory.com/406
sort는 버블 정렬식으로 두 수를 비교하여 점차 뒤로 움직인다.
var failure: [Int: Float] = [1: 1, 2: 2, 4: 3, 3: 2]
print(failure.sorted(by: <).sorted(by: { $0.value > $1.value }).map { $0.key })
// [4, 2, 3, 1]이런식으로 이중 sort도 가능하다. 일단 1234로 정렬한 뒤 value값에 따라 두 수를 버블 정렬식으로 비교하면 value값을 내림차순으로 정렬하면서도 value값이 같다면 key 값은 오름차순이 된다.
var n = 123
let binary = String(n, radix: 2)zip은 두 개의 시퀀스로 구성된 시퀀스쌍을 만든다.
let words = 1...4
let numbers = 1...4
for (word, number) in zip(words, numbers) {
print("\(word): \(number)")
}
1: 1
2: 2
3: 3
4: 4https://hyunsikwon.github.io/swift/Swift-Inout-01/
https://jud00.tistory.com/entry/오늘의-Swift-지식-if-let-과-guard-let의-차이는
var a: Set<Int> = [1, 2, 3]
a.remove(1)
print(a) // [2, 3]You can remove specific numbers in Set
let a = "try hello world"
let b = a.components(separatedBy: " ").map { $0.enumerated().map { ($0.element, $0.offset) } }
print(b)
// [[("t", 0), ("r", 1), ("y", 2)], [("h", 0), ("e", 1), ("l", 2), ("l", 3), ("o", 4)], [("w", 0), ("o", 1), ("r", 2), ("l", 3), ("d", 4)]]var a = [["T", "r", "Y"], ["H", "e", "L", "l", "O"], ["W", "o", "R", "l", "D"]]
print(a.map { $0[0] })
//["T", "H", "W"]var a = [[2, 2, 3], [3, 3], [34]]
print(a.filter { $0[0] * 2 == 4 }) // [[2, 2, 3]]
print(a.flatMap { $0 }) // [2, 2, 3, 3, 3, 34]var arrTwoDimension = [[Int]]()
var arrTwoDimension: [[Int]] = []switch문에서 continue 써도 for문 하나를 건너뛴다.
https://dmsitter.tistory.com/136