Swift has gone from ABI 5.0 to module 5.1 stability, and Swift is finally not Swift start to Learn again. WWDC2020, Swift 5.3 official release, Swift is still in the direction of security, efficient, easy to read and continue to improve the syntax, enhance the expression of code and ease of use. Because Swift’s modules are stable, SPM now supports the distribution of binary modules, and the evolving community ecosystem is expanding the range of areas Swift can reach beyond the Apple platform.

Swift Development Milestone

The image below shows a brain map of Swift content in WWDC2020 to help you quickly understand it.

Improvement and expansion of the language environment

A complete programming language has three basic elements: syntax, a standard library, and debugging capabilities. Syntax design determines the programming paradigm of a language; The standard library determines the basic capabilities of a language; The ability to debug determines the developer experience and the stability of the language.

As Apple continues to reinforce these points in the Swift iteration, we can see how Swift has improved.

Grammatical features

The core of Swift’s syntax design is OOP, but this does not prevent Swift’s syntax from being enhanced with support for POP, functional programming and even DSL. Swift also began to adapt to the direction of declarative programming due to the addition of SDL features, such as @main mentioned later.

Multiple trailing closure

This improvement solves the problem of nesting parentheses when the last parameters of a function are closures, making the API cleaner and more expressive. SwiftUI takes advantage of this language feature to make it simpler and easier to understand.

KeyPath as Function

KeyPath<Target, Value> -- implicit conversion --> (Target) --> ValueCopy the code

KeyPath can now be used as a function. The problem with this syntactic sugar is that when we use a function like map, we only need to fetch a property in the corresponding data model, so we have to write code like map {$0. Property}. With this syntactic sugar, things can be simplified to map(.property).

Type-based program entry point (@main)

A new modifier, @main, was introduced to mark all types implemented with public static func main(), whether extended or inherited from main. The point of adding this feature is to maintain declarative semantics to the end. YES!

Increase availability of implicit self in closures

In the past, when we wrote the escape closure, if we caught self, we needed to write self in relation to self. To alert us to circular references.

If you declare capture self in the closure’s capture list, self-related access in the closure can be omitted. If accessed in an immutable function, self can be omitted (for SwiftUI).

This is useful but not very extensive, because in practice, we try to weakly reference self and then strongly reference self to keep self accessible. This optimization is not available in the following scenarios:

requestDataAsync { [weak self] in
    guard let self = self else {return} self.property // ✅ property // ❎ need to explicitly declare self}Copy the code

Multi-pattern catch clauses

This way of writing can automatically implement error matching, enter the corresponding error handling, without the need to use switch, improve the readability of the language.

Enum enhancements

Automatic compliance Comparable

The compiler can now automatically generate Comparable methods for you to implement Enum comparisons.

Enum case can be used to match protocol

This feature to see the reference scene needs it, the official gave a better example. For details, see SE-0280.

New DSL support for Switch

Switch-case can now be used for pattern matching in Swift DSLS such as SwiftUI, previously only if-else support was available. It’s also kind of a SwiftUI ability.

The standard library

In fact, it is said that the standard library is a broad sense of the standard library, including developers with the language distribution of the standard library, such as the standard I/O library, runtime environment, compilation environment, party library, etc. Apple is working hard on that this year, because the standard library and various third-party libraries are the place to show a language’s ability. Otherwise, no one will use a good grammar. Language is still a tool, and solving problems is the key.

Swift 5.3 has a significant increase in code size and runtime. Swift Package Manager (SPM) added support for binary and resources, deep integration with Xcode, and the advantages of parent sons gradually showed up. Swift was originally designed as a General Purpose language. This year, Apple officially announced Swift support for Apple Platform, Ubuntu/CentOS/Amazon Linux, and Windows in the near future. Officially a good cross-platform language.

Standard library update

Float16 support, with better performance

Apple Archive is a compressed file that functions like ZIP and is used by Apple to update its systems

Swift System wraps the basic API of the operating System, making the API more robust and easy to use

OSLog Recommended logging system

These updates show that Apple is very concerned about Swift’s underlying operations, and the Swift System allows developers using Swift’s underlying operations to get a more consistent and robust code experience without the OS C API.

New party library

Numerics solves problems related to mathematical calculations in Swift

ArgumentParser provides powerful tools for writing scripts for Swift

StandardLibraryPreview previews new features in the standard library

Code Size and Runtime

In the case of UIKit, the binary generated for Swift 4.1 has gone from more than twice as much as OC, but in Swift 5.3 the gap has narrowed to less than 1.5 times. This means that the binary size is automatically optimized for heavy use of Swift apps, which is now less of a problem for apps worried about Swift causing packet size issues. The security and development experience provided by Swift is available at an acceptable price.

If the App uses pure SwiftUI, the binary code can be shrunk by as much as 43%. You can see the depth of Apple’s Swift optimization, and these optimizations, just need to compile again to enjoy, why not.

Due to Swift’s more compact value types, runtime memory, allocating the same object naturally requires less space than OC. Swift 5.3 stores much less of the necessary additional information at runtime than 5.1, and even less than OC, significantly reducing Swift’s runtime memory. This is very helpful for low-memory devices, and at the same time, less system memory means more user memory. All you need to do is recompile.

Swift’s underlying optimizations also make it an efficient language that reduces runtime requirements to improve its application scenarios.

Swift Package Manager

SPM, as a very important part of Swift ecology, has also been renewed.

SPM supports binary packet distribution

SPM supports resource packaging

These two updates have shown that the CAPABILITIES of the SPM are fully developed. At present, internal modules of apps with a certain scale begin to use Cocoapods for binary component-based integration, which can clearly decouple the code and improve the efficiency of packaging. In this context, SPM’s support for these two key features can already cover the needs of large apps, and SPM can not only play with Swift, but also C Family.

In the comparison between SPM and Cocoapods, the parent son SPM is deeply integrated with Xcode, which can directly open and edit Swift package. Xcode has a corresponding operation interface designed by SPM, reducing the threshold of development and use. The mature tool chain also makes it easy to tune Swift Package. However, Cocoapods is maintained by the community and does not respond very quickly to every Xcode update. There are many inconsistencies for large apps due to the separation of Podfiles and PodSpecs.

Now might be a good time to embrace the SPM.

cross-platform

The list of officially supported operating systems is as follows:

Apple platform

Ubuntu 16.04, 18.04, 20.04

CentOS 8

Amazon Linux 2

Windows (coming soon)

Truly cross-platform, and Swift officially supports AWS Lambda. AWS Lambda Runtime is open source and supports AWS FaaS programming, further broadening Swift’s reach.

Debugging capability (Developer experience)

Debugging and developer experience is also a very important part of a language, because no one writes error-free code, and the ability and tools to check for errors are very important to a language. Apple also pays a lot of attention to this aspect, putting a lot of effort into the developer experience.

More intelligent diagnostic information

Developers new to Swift can often be confused by Xcode error messages, especially with the introduction of SwiftUI. When THE author wrote SwiftUI for the first time, whenever there was a mistake somewhere in the body, the error reported was incorrect. Only through visual examination and inference could I understand my mistake, which was very painful.

Now That Apple has reworked the diagnostic capabilities, Swift error diagnostics are much more accurate than before, errors are not reported incorrectly and error notifications are easy to understand, especially for SwiftUI, where it’s easy to see what’s wrong. In Swift, compiling through is a good proof of correctness, unless you shut the compiler up in an unsafe way.

Automatic completion

The enhanced type inference system also enhances Swift’s code completion capability. This will be easy to experience when you upgrade to Xcode 12. The ability to indent code has also been enhanced.

LLDB

Embrace Swift

After watching WWDC for so many years, everyone should have a mentality every time they watch it

We only support the latest version of iOS X (lower version), and I don’t care about that stuff

Swift smells good, but reality only makes me use OC (sigh)

What language is not used, OC must be enough for so many years

The crisis

However, if you don’t make the change in time and keep it working, you will be carrying more and more burden along the way. When you find that you can’t move fast, you will look back at WWDC and find that the solution has been given out for a long time. But at that time, it didn’t seem to be a problem, which is not supported and inappropriate. But now when you want to use it, you have no idea what to do when you face the chaos on your back.

Making a change is painful, but it can be even more painful when what used to be a tickle and a fix becomes a current pain point.

Swift emerged to replace and surpass objective-C language. Although Apple still uses OC due to historical reasons, there are signs that Apple is doing positive work to gradually reduce the proportion of OC in the whole system through Swift.

The community is also actively changing, many well-known third party libraries have migrated to Swift and the OC version is no longer maintained, for example Lottie has features on the Swift version that are not present in the OC version and the OC version is no longer maintained. This phenomenon will gradually increase.

change

We are also actively exploring the landing of Swift Mobile, and have achieved phased achievements in the correct operation of Swift 5.1 module in Mobile.

Now the time is ripe, the language features, SPM, toolchain, and standard library are powerful enough, and it’s time to make a change.

Although Swift looks very simple, in fact it is a language with low lower limit and high upper limit. The Swift environment within the group needs to be maintained by all of us. We will also strengthen training related to Swift language in the future, so that developers can truly understand Swift, learn Swift, and become a Swifter instead of an OSwifter.

Reference:

Behind SwiftUI

WWDC20 What’s new in Swift

WWDC19 What’s new in Swift

WWDC18 What’s new in Swift

WWDC17 What’s new in Swift

WWDC16 What’s new in Swift

WWDC20 Swift packages: Resources and localization

WWDC20 What’s new in SwiftUI

What are the opportunities and challenges for Swift 5?

Swift Evolution

Mobile client team

The mobile app team is recruiting for the position of iOS and Android client development engineer. We welcome your recommendation. Resume: [email protected]