Swift 5 新功能

Table of Contents

ABI compatability

Standar Result type

Raw string

Customizing string interpolation

@dynamicCallable

Handling future enum cases

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(),感觉像两者的结合:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值