Swift 5.7: Regex. Shorthands for optional unwrapping. Unlock existentials for all protocols.
Shorthands for optional unwrapping
if let workingDirectoryMailmapURL {
print (workingDirectoryMailmapURL)
}
while let workingDirectoryMailmapURL {
print (workingDirectoryMailmapURL)
break
}
guard let workingDirectoryMailmapURL else {
return
}
print (workingDirectoryMailmapURL)
Improved type inference for complex closures
Swift has recently enhanced its type inference capabilities for closures. It can now automatically infer the return type of complex closures, as demonstrated by the following example:
let scores = [100, 70, 75]
let results = scores.map { score in
if score >= 75 {
return "\(score)%: Pass"
} else {
return "\(score)%: Fail"
}
}
In this closure, which uses the map function to transform an array of scores into an array of corresponding pass/fail statements, Swift is now able to accurately infer the return type as String, without requiring explicit type annotations.
Better string parsing with Swift Regex
Regular expressions (Regex) are powerful tools for pattern matching and string manipulation. Regexes, though very compact, can sometimes be difficult to understand and debug.
Here’s an example of how to use Swift Regex to extract hashtags from a tweet:
import RegexBuilder
if let regex = try? Regex("#[a-zA-Z0-9_]+") {
let matches = tweet.matches(of: regex)
for match in matches {
let hashTag = tweet[match.range.lowerBound ..< match.range.upperBound]
print (hashTag)
}
}
In this example, we import the RegexBuilder library, which provides a convenient way to construct regular expressions in Swift. We then define a regex pattern that matches hashtags (#[a-zA-Z0–9_]+), wrap it in a try? statement to handle any parsing errors that may occur, and apply it to the tweet string using the matches(of:) method.
Creating a Regex with Regex literal
One of the most straightforward methods to create a Regex is using Regex literals. The code below demonstrates how to use Regex literals, which offer compile-time checks. You can learn more about Regex literals?here.
import RegexBuilder
let regex = /#[A-Za-z_0-9]+/
let matches = tweet.matches(of: regex)
for match in matches {
let hashTag = tweet[match.range.lowerBound ..< match.range.upperBound]
print (hashTag)
}
Creating a Regex using RegexBuilder
RegexBuilder is a domain-specific language that provides an expressive result-builder-based way to build regular expressions. It is similar to SwiftUI.
To use it, you first need to import the RegexBuilder module. Then, you can create a regular expression using the Regex closure.
As an example, consider creating a regex that matches hashtags in a tweet. The regex should begin with the ‘#’ character, followed by one or more characters from the set [a-z0–9_]. Here’s how you can create this regex using RegexBuilder:
import RegexBuilder
let regex = Regex {
"#"
OneOrMore {
CharacterClass(
("a" ... "z"),
("0" ... "9"),
("_" ... "_")
)
}
}
let matches = tweet.matches(of: regex)
for match in matches {
let hashTag = tweet[match.range.lowerBound ..< match.range.upperBound]
print (hashTag)
}
Unlock existentials for all protocols
Swift 5.7 significantly loosens the Swift’s ban on using protocols as types when they have associated type requirements. In simple terms, this means the following code becomes legal:
let tvShow: [any Equatable] = ["London", 99]
Swift Regex Literals
Implemented: Swift 5.7
Full Version / DEV Community / Medium
Contacts
I have a clear focus on time-to-market and don't prioritize technical debt. And I took part in the Pre-Sale/RFX activity as a System Architect, assessment efforts for Mobile (iOS-Swift, Android-Kotlin), Frontend (React-TypeScript) and Backend (NodeJS-.NET-Kafka-SQL-NoSQL). And I also formed the work of Pre-Sale as a CTO from Opportunity to Proposal via knowledge transfer to Successful Delivery.
?? Email:?[email protected]
?? LinkedIn:?https://www.dhirubhai.net/in/sergeyleschev/
?? LeetCode:?https://leetcode.com/sergeyleschev/
?? Twitter:?https://twitter.com/sergeyleschev
?? Github:?https://github.com/sergeyleschev
?? Website:?https://sergeyleschev.github.io
?? DEV Community: https://dev.to/sergeyleschev
?? Reddit: https://reddit.com/user/sergeyleschev
?? Quora: https://quora.com/sergey-leschev
?? Medium: https://medium.com/@sergeyleschev
??? PDF Design Patterns:?Download