Swift 5.0 Release - kirseia/study GitHub Wiki
- now
let result = Result { try String(contentsOfFile: someFile) }
- ์ด๋ ๊ฒ ์ฌ์ฉ ๊ฐ๋ฅ, ๊ฒฐ๊ณผ๋ฅผ Result๋ก ๊ฐ์ธ์ ์ฌ์ฉ ๊ฐ๋ฅ
- functional programming ํ ๋ error chaining ๊ฐ์ ๋ ์ ์ฉํ ๊ฒ ๊ฐ๋ค.
- Result ํ์ฉ๋ฒ
- before
print("<a href=\"\(url)\" title=\"Apple Developer\">")
- now
print(#"<a href="\#(url)" title="Apple Developer">"#)
let answer = 42
let dontpanic = #"The answer to life, the universe, and everything is \#(answer)."#
- escaping ์ ์ํด๋ ๋๋ค๋ ์ฅ์ ์ ์์. ํน๋ณํ ์ํฉ์์ ์ ์ฉํ ๋ฏ
- now
struct User {
var name: String
var age: Int
}
extension String.StringInterpolation {
mutating func appendInterpolation(_ value: User) {
appendInterpolation("My name is \(value.name) and I'm \(value.age)")
}
}
let user = User(name: "Guybrush Threepwood", age: 33)
print("User details: \(user)")
- ํธํ ๋๊ฐ ์๊ธด ํ๋ฐ...
- CustomStringConvertible ์ ์จ๋ ๋ ๊ฒ ๊ฐ๊ธด ํ๊ณ ...
- before
// Swift:
7 % 2 == 1 // true
-7 % 2 == 1 // false. -7 % 2 evaluates to -1
// Ruby and Python
7 % 2 == 1 // true
-7 % 2 == 1 // true
- now
let rowNumber = 4
if rowNumber.isMultiple(of: 2) {
print("Even")
} else {
print("Odd")
}
- ์์์ผ ๋ ๋ฌธ์ ๊ฐ ์๊ธด ํ์ผ๋ ... ์ด๊ฒ๋ ์
- xcode 10.2์์ ์ ๊ฑฐ ๋๋ค๊ณ ํจ.
- now
@dynamicCallable struct ToyCallable {
func dynamicallyCall(withArguments: [Int]) {}
func dynamicallyCall(withKeywordArguments: KeyValuePairs<String, Int>) {}
}
let x = ToyCallable()
x(1, 2, 3)
// Desugars to `x.dynamicallyCall(withArguments: [1, 2, 3])`
x(label: 1, 2)
// Desugars to `x.dynamicallyCall(withKeywordArguments: ["label": 1, "": 2])`
- ์ ๋ชจ๋ฅด๊ฒ ์
- ์ ์ฉํ๊ฒ ์ฌ์ฉํ๋ ์ฝ๋๋ฅผ ๋ด์ผ ์๊ฒ ๋ ๊ฒ ๊ฐ์
- ๋ณ์๋ฅผ ํจ์์ฒ๋ผ ์ฌ์ฉํ๋ค๋๋ฐ
let id = \Int.self
var x = 2
print(x[keyPath: id]) // Prints "2"
x[keyPath: id] = 3
print(x[keyPath: id]) // Prints "3"
- ์ ์ฒด ์ ๋ ฅ๊ฐ ์ฐธ์กฐํ๋ KeyPath๊ฐ ์์๋๋ฐ (์์ ์ ์ฐธ์กฐํ๋) .self KeyPath๊ฐ ์ถ๊ฐ๋จ
- before
enum X {
case foo(bar: Int...)
}
โจfunc baz() -> X {
return .foo(bar: 0, 1, 2, 3)
}
- now
enum X {
case foo(bar: [Int])
}
โจfunc baz() -> X {
return .foo(bar: [0, 1, 2, 3])
}
- ์๋๋์ง ์์๋ ๊ฒ์ด๋ผ ๋ง์๋ค๊ณ ํจ.
- ๋์ ๋ฐฐ์ด์ ์ ๋ฌํ๋๊ฒ์ด ๊ฐ๋ฅํด์ง
let a: Set<Int> = [1, 2, 3, 4, 5]
let b: Set<Int> = [1, 2, 3, 4, 5]
a == b // true
print(a) // [1, 4, 3, 2, 5]
print(b) // [4, 2, 5, 1, 3]
- ์์ฑ ๋ ๋ ์๋ ๋น์ทํ์๋๋ด. ์์ผ๋ก ์์ฑ ํ ๋ ์์๊ฐ ๋ ๋ฌ๋ผ์ง๊ฑฐ๋ผ๊ณ ํจ.
The Sequence protocol no longer has a SubSequence associated type. Methods on Sequence that previously returned SubSequence now return concrete types. For example, suffix(_:) now returns an Array. (45761817)
- ๋ญ์ง ์ ๋ชจ๋ฅด๊ฒ ์.
#if swift(<4.2)
// This will only be executed if the Swift version is less than 4.2.
#endif
#if compiler(<4.2)
// This will only be executed if the Swift compiler version is less than 4.2.
#endif
- ๊ธฐ์กด์ >= ๋ง ์์๋๋ฐ ์ ์ฉํ ๊ฒ ๊ฐ์
- before
let d: [String: String?] = ["a": "1", "b": nil, "c": "3"]
let r1 = d.filter { $0.value != nil }.mapValues { $0! }
let r2 = d.reduce(into: [String: String]()) { (result, item) in result[item.key] = item.value }
// r1 == r2 == ["a": "1", "c": "3"]
- now
let d: [String: String?] = ["a": "1", "b": nil, "c": "3"]
let r4 = d.compactMapValues({$0})
// r4 == ["a": "1", "c": "3"]
- ๊ธฐ์กด array ์๋ง ์ฌ์ฉํ๋ compactValues๊ฐ dictionary ์๋ ์ถ๊ฐ ๋จ
// Swift 4: 'Int??'
// Swift 5: 'Int?'
let result = try? database?.countOfRows(matching: predicate)
// Swift 4: 'String??'
// Swift 5: 'String?'
let myString = try? String(data: someData, encoding: .utf8)
// Swift 4: '[String: Any]??'
// Swift 5: '[String: Any]?'
let dict = try? JSONSerialization.jsonObject(with: data) as? [String: Any]
- ๊ธฐ์กด์ optional ๋ก ๋์ค๋๊ฒ 2์ค optional๋ก ๋์์ ์ฌ์ฉํ๊ธฐ ๊ท์ฐฎ์๋๋ฐ ์ข์์ง ๋ฏ
func processImageData(completionBlock: (result: Image) -> Void) {
loadWebResource("dataprofile.txt") { dataResource in
loadWebResource("imagedata.dat") { imageResource in
decodeImage(dataResource, imageResource) { imageTmp in
dewarpAndCleanupImage(imageTmp) { imageResult in
completionBlock(imageResult)
}
}
}
}
}
์ด๋ฐ ์ฝ๋๋ฅผ ->
func processImageData() async -> Image {
let dataResource = await loadWebResource("dataprofile.txt")
let imageResource = await loadWebResource("imagedata.dat")
let imageTmp = await decodeImage(dataResource, imageResource)
let imageResult = await dewarpAndCleanupImage(imageTmp)
return imageResult
}
- ์ด๋ ๊ฒ ๋ฐ๊พธ์๋ ๋ ผ์๊ฐ ์์์ผ๋ ๋์ ๋์ง ์์.
- funtional programming ๊ณผ๋ ์ด์ธ๋ฆฌ์ง ์๋ ๋ค๋ ์ด์ ๋๋ฌธ์ผ๊น ..?
The String structureโs native encoding switched from UTF-16 to UTF-8, which may improve the relative performance of String.UTF8View compared to String.UTF16View. Consider re-evaluating any code specifically tuned to use String.UTF16View for performance. (42339222)
- ์๋ ํฅ์ ์ฝ๊ฐ ์๋ค๊ณ ํจ