Swift 5 What’s new?

Tram Ho

) { $0[$1.key] = Int($1.value) } // [“Oana”: “10”]

Swift 5 dùng compactMapValues(_:) để giải quyết vấn đề trên (xem chi tiết tại đây):

7. Renaming Dictionary Literals

Swift 4.2 dùng DictionaryLiteral để khai báo cho 1 từ điển:

Note: DictionaryLiteral không phải là một dictionary hoặc 1 literal. Nó là 1 list các cặp key-value.

Swift 5 đã đổi tên DictionaryLiteral thành KeyValuePairs: let pets: KeyValuePairs = [“dog”: “Sclip”, “cat”: “Peti”]

8. Handling Future Enumeration Cases

swift 4.2:

Trong swift 5 sẽ bổ sung đánh dấu các giá trị đang để mặc định để trong tương lai bạn có thể thay các giá trị đang là default của enum thành các giá trị cụ thể bằng cách báo warning của Xcode:

In this code, you mark default as @unknown, and Swift warns you that switch isn’t exhaustive. default handles .screencast, .course and .podcast because screencasts, courses and podcasts are blog posts. Trong đoạn code trên, bạn đánh dấu @unknown, và swift sẽ báo warning rằng : Switch must be exhaustive. Xcode sẽ implement tất cả các enumeration cases của Enum.

9. Adding Result to the Standard Library

Swift 5 thêm Result vào standard library:

  • Kiểu Result type handle kết quả của asynchronous function dùng enum với 2 case: success and failure. Để hiểu nó như thế nào mình sẽ đưa ra 1 ví dụ xử lý kết quả trả về khi gọi API:

Từ đoạn code trên để xử lý kết quả trả về chúng ta cần dùng if let hoắc guard để check response status code, data trả về , and decode the data ra thành model. Cách xử lý thanh hơi cồng kềnh, phức tạp. OK! bây giờ các bạn thử dùng type Result trong swift 5 xem nó như thế nào nhé:

Cách này nhìn code khá là clear và dễ hiểu phải không nào.

10. Platform Deployment Settings

Swift 5 cho phép bạn định nghĩa số các platform tối thiểu trong Package.swift

Bạn có thể dùng macOS(), iOS(), tvOS() và watchOS() trong SupportedPlatform để set flatform tối thiểu trong Package.swift.

11. Flattening Nested Optionals

Swift 4.2 tạo nested optionals with try? như sau:

Tuy nhiên khi lên swift 5 thì nested optionals with try?

try? trong swift 5 sẽ không tạo nested optionals , bạn chỉ cần unwrap division 1 lần duy nhất khi nó đang là kiểu Int?

12. Build Configuration Updates

Swift 4.2 dùng >= trong compilation conditions:

Swift 5 support thêm dấu < trong compilation conditions:

Tài Liệu Tham Khảo

https://www.raywenderlich.com/55728-what-s-new-in-swift-5https://medium.com/@azamsharp/understanding-result-type-in-swift-5-387d5ef9f45e

  Swift 5 Có gì mới ?

Swift chen chân vào top 10, Java vẫn giữ vững ngôi vương””]

  Swift 5 Có gì mới ?

Apple có ngôn ngữ lập trình Swift thì giờ đây Google cũng có ngôn ngữ lập trình “hạng nhất” cho Android”]

Swift 5 released on March 25, 2019, there are many new features such as Escaping Raw Strings, handling future enumeration, result type … Today I would like to introduce you to read some new features of swift 5 compared. with swift 4.2. Ok, let’s find out!

10-month TIOBE Index – Swift lost its position
Working with Model and Dictionary in ObjC and Swift (part 1)

1. Check integer multiples

Example: To divide a by number in Swift 4.2, you must check whether b is different from 0 or not as follows:

In swift 5: you just need to use isMultiple (of 🙂 function to check if the number a is a multiple of b number?

2. Escaping Raw Strings

In Swift 4.2, Escape sequences are using the backslashes () and quote marks (“) in strings.

Swift 5 can add # at the start and end position of string string when you use backslashes and quote marks

Khi sử dụng chuỗi kết hợp với dữ liệu chuỗi, bạn cần dùng một ký tự bảng ở sau backslash: When using string interpolation in raw string VD: "raw string (a)" , you can use # after the sign

Note: You can use multiple # sign before the sign “or after the sign

Print Swift 4.2, you escape backslashes inside regular expressions as follows: In swift 4.2, you escape backslashes ( ) in regular expressions:

In swift 5 you just write one half slash by replacing by #

3. Use the new character property

In swift 4.2, you convert 1 chracter from String to Int, you do the following:

However, Swift 5 adds the isNumber attribute to check if a character is an Int.

You can go here to see the new character attributes.

4. Use the new Unicode Scalar Properties

In Swift 4.2, you can check 1 character for example if the character ‘a’ is not a number or not in the following way:

Swift 5, with new properties, makes handling simpler by using isAlphabetic attributes:

5. Delete Subsequences

In swift 4.2, Subsequences will be returned when Custom Sequences.

Swift 5, has replaced Subsequences into concrete types (specific types):

6. Compacting Dictionaries

Swift 4.2, use mapValues, filter and reduce to filter the values ​​= nil from dictionary as follows:

Swift 5 uses compactMapValues(_:) to solve this problem ( see details here ):

7. Renaming Dictionary Literals

Swift 4.2 uses DictionaryLiteral to declare for a dictionary:

Note : DictionaryLiteral is not a dictionary or a literal. It is a list of key-value pairs.

Swift 5 renamed DictionaryLiteral to KeyValuePairs: let pets: KeyValuePairs = [“dog”: “Sclip”, “cat”: “Peti”]

8. Handling Future Enumeration Cases

Swift 4.2:

In swift 5 will add the value markings that are default so that in the future you can replace the default values ​​of enum to specific values ​​by warning the warning of Xcode:

In code this, you mark default as @unknown , and Swift warns you are not exhaustive. default handles .screencast, .course and .podcast because of the screencasts, courses and podcasts are blog posts. In the above code, you mark @unknown , and swift will warn that: Switch must be exhaustive. Xcode will implement all of Enum’s enumeration cases .

9. Adding Result to the Standard Library

Swift 5 adds Result to the standard library :

  • The Result type handle results in asynchronous function using enum with 2 cases: success and failure. To understand how I will give an example to handle the result returned when calling the API:

From the above code to handle the returned result we need to use guard if let to check response status code, return data, and decode the data to model. How to handle the bar slightly bulky, complex. OK! Now how do you try to use the type Result in swift 5 to see it:

This way of looking at the code is quite clear and easy to understand, right.

10. Platform Deployment Settings

Swift 5 allows you to define the minimum number of platforms in Package.swift

You can use macOS (), iOS (), tvOS () and watchOS () in SupportedPlatform to set the minimum platform in Package.swift.

11. Flattening Nested Optionals

Swift 4.2 creates nested optionals with try? as follows:

However, when uploading to swift 5 is nested optionals with try?

try? in swift 5 will not create nested optionals, you just need to unwrap division once only when it is Int?

12. Build Configuration Updates

Swift 4.2 uses> = in compilation conditions:

Swift 5 support adds <in compilation conditions:

References

https://www.raywenderlich.com/55728-what-s-new-in-swift-5 https://medium.com/@azamsharp/understanding-result-type-in-swift-5-387d5ef9f45e

Swift 5 What’s new?

Swift stepped into the top 10, Java still held the throne. “]]

Swift 5 What’s new?

Apple has a Swift programming language, Google now has a “first-class” programming language for Android “]

Share the news now

Source : viblo