Swift 4.0 Update collection, new feature rollup

Source: Internet
Author: User

This article by Chen Yunfeng translation, reprint please specify.

Note: This tutorial will be bundled with the Swift 4 version in Xcode 9 Beta 1.

Swift 4 is the latest version Apple plans to launch in the fall of 2017, with a major focus on providing source compatibility with Swift 3 code and striving to achieve ABI stability.

This article focuses on changes to Swift that will have the greatest impact on your code. And, let's get started. Getting Started

Swift 4 is included in Xcode 9. You can download the latest version of Xcode 9 from Apple's developer portal (you must have an active developer account). Each Xcode beta will bundle the latest Swift 4 snapshots at the time of release.

As you read, you will notice the links in the [se-xxxx] format. These links will take you to the relevant swift evolution proposal. If you want to learn more about any topic, be sure to check it out.

I suggest you try every Swift 4 feature or update on the playground. This will help solidify the knowledge in your mind and give you the ability to learn more about each topic. Try to enlarge/break their example to play with these examples. Have fun.

Note: This article will be updated for each Xcode beta. If you use a different swift snapshot, the code here is not guaranteed to work. migrate to Swift 4

Migrating from Swift 3 to 4 will be more troublesome than 2.2 to 3. In general, most changes are added and should not require a great deal of personal feeling. Therefore, the swift migration Tool will handle most of the changes for you.

Xcode 9 also supports Swift 4 and Swift 3 intermediate versions in the Swift 3.2. Each target in your project can be swift 3.2 or Swift 4, and you can migrate individually if you want. However, converting to Swift 3.2 is not completely free – you may need to update the Code section to be compatible with the new SDK, and since Swift has not yet Abi stable, you will need to recompile the dependencies using Xcode 9.

When you are ready to migrate to Swift 4 o'clock, Xcode again provides a migration tool to help you. In Xcode, you can navigate to edit/convert/to the current swift syntax ... To start the conversion tool.

After you select the target you want to convert, Xcode will prompt you for a preference for objective-c inference. Select the recommended option to reduce the binary size by restricting references (for more information about this topic, see the following restrictions @objc inference)

To better understand the changes that are expected in your code, we will first introduce the API changes in Swift 4. API Changes

Before jumping to the additions introduced in Swift 4, let's take a look at the changes/improvements made by the existing APIs. string

String has received a lot of good love in Swift 4. This proposal contains a lot of changes, so let's decompose the biggest. [SE-0163]:

If you feel nostalgic, strings are collected again, just as they were before Swift 2.0. This change eliminates the need for a string array on a string. You can now iterate directly over a string object:

Let Galaxy = ' milky Way ' for
Char in Galaxy {
  print (char)
}

Not only can you iterate through string logic, but you can also get all the bells and whistles from sequence and collection:

Galaxy.count       //
Galaxy.isempty     //False
Galaxy.dropfirst ()//"Ilky Way"
String (  Galaxy.reversed ())//"Yaw Yklim"

//Filter out any none-ASCII characters
Galaxy.filter {char in-let
  isascii = Char.unicodeScalars.reduce (True, {$ && $1.isascii}) return
  isascii
}//"Milky Way"

The above ASCII example shows a pair of character. You can now access character directly from character. Previously, you needed to instantiate a new string [SE-0178].

Another one is stringprotocol. It declares most of the features previously declared on a string. The reason for this change is to improve how slices work. Swift 4 Adds a substring type that references a subsequence on a string.

string and substring implement Stringprotocol so that they have almost the same functionality:

//Grab a subsequence of String let Endindex = Galaxy.index ( Galaxy.startindex, offsetby:3) var milksubstring = Galaxy[galaxy.startindex...endindex]//"Milk" type (Of:milksubstrin g)//Substring.type//concatenate a string onto a Substring milksubstring + = ""//"Milk"//Create a string fro M a Substring let milkstring = String (milksubstring) 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.