Table of Contents
Customizing string interpolation
Flattening nested optionals resulting from try?
Transforming and unwrapping dictionary values with compactMapValues()
本文是笔记,不是完整的翻译,请参考原文:https://www.hackingwithswift.com/articles/126/whats-new-in-swift-5-0
Xcode版本:Version 10.2 (10E125)
ABI compatability
ABI stability千呼万唤始出来
Standar Result type
新的Result类型,适合用于返回异步操作的结果,是作为带有associated value的enum来实现的:
可以很方便地创建:
Raw string
raw string中,\ and “ 作为literal而不是转义字符或者string结束符来处理
Regex(正则表达式)尤为受益
Customizing string interpolation
原来这样:
现在可以这样:
还有更复杂的应用,详见原文
@dynamicCallable
@dynamicCallable is the natural extension of @dynamicMemberLookup, and serves the same purpose: to make it easier for Swift code to work alongside dynamic languages such as Python and JavaScript.
以前这么写:
现在可以这样:
两个method,可以定义一个,也可以都用
Handling future enum cases
在switch的default前面加个@unknown,这样如果enum加了新值,会warn说switch不exhaustive
Flattening nested optionals resulting from try?
modifies the way try? works so that nested optionals are flattened to become regular optionals. This makes it work the same way as optional chaining and conditional typecasts, both of which flatten optionals in earlier Swift versions.
Swift 4.2及更旧版本,messages是String??,现在是String?
Transforming and unwrapping dictionary values with compactMapValues()
Array有个compactMap(), Dictionary有个mapValues(),为Dictionary新加了这个compactMapValues(),感觉像两者的结合: