Swift 5.4, built into Xcode 12.5, adds several new features.
Improved implicit member syntax
[Bug Mc-108226] – Setting colors in UIKit and SwiftUI cannot be passed directly. In Swift 5.4, this syntax has been improved to omit prefixes and support chained calls.
- UIKit
let view = UIView()
view.backgroundColor = .red.withAlphaComponent(0.5)
Copy the code
- SwiftUI
struct ContentView: View {
var body: some View {
Text("Swift, 5.4")
.foregroundColor(.red.opacity(0.5))
.padding()
}
}
Copy the code
Multiple variable parameters are supported
Prior to Swift 5.4, functions could only have one variable parameter. Now, multiple variable parameters are supported.
// Multiple variable parameters
func score(courses: String. .scores: Int...). {
for i in 0 ..< courses.count {
print(""\(courses[i])Course results:\(scores[i])")}}/ / call
score(courses: "Swift"."IOS development"."SwiftUI", scores: 90.95.100)
Copy the code
Nested functions support overloading
Prior to Swift 5.4, normal functions could be overloaded; now nested functions can also be overloaded.
func method(a) {
// insert function 1
func add(num1: Int.num2: Int) -> Int {
num1 + num2
}
// insert function 2
func add(num1: Int.num2: Int.num3: Int) -> Int {
num1 + num2 + num3
}
// insert function 3
func add(num1: Double.num2: Double) -> Double {
num1 + num2
}
// insert function 4
func add(a num1: Int.b num2: Int) -> Int {
num1 + num2
}
add(num1: 10, num2: 20) / / 30
add(num1: 10, num2: 20, num3: 30) / / 60
add(num1: 10.0, num2: 20.0) / / 30
add(a: 10, b: 20) / / 30
}
method()
Copy the code
Result builders
- Swift 5.4 before callingFunction builders, it uses one
buildBlock
Method can be used toMore than contentBuild forA resultThis feature is widely used on SwiftUI. - You can use
@resultBuilder
Customize Result Builders.
@resultBuilder
struct StringBuilder {
// buildBlock builds multiple values into a single result
static func buildBlock(_ strs: String...). -> String {
// Concatenate multiple strings with a newline character
strs.joined(separator: "\n")}// if logic branch
static func buildEither(first component: String) -> String {
return "if \(component)"
}
// else logical branch
static func buildEither(second component: String) -> String {
return "else \(component)"}}@StringBuilder
func buildString(a) -> String {
"Thoughts of the Still Night"
"Tang Li Bai"
"The moon was shining before the bed, and I thought it was frost on the ground."
"Look up at the bright moon, bow to think of home."
if Bool.random() {
"A poem"
} else {
"A word"}}print(buildString())
Copy the code
Local variables support attribute wrapping
Swift 5.4 supports attribute wrappers introduced in Swift 5.1 to local variables.
// Attribute wrapping
@propertyWrapper struct Trimmed {
private var value: String = ""
var wrappedValue: String {
get { value }
set { value = newValue.trimmingCharacters(in: .whitespacesAndNewlines) }
}
init(wrappedValue initialValue: String) {
wrappedValue = initialValue
}
}
struct Post {
func trimed(a) {
// Local variables
@Trimmed var content: String = "Swift 5.4 Property Wrappers"
print(content)
}
}
class ViewController: UIViewController {
override func viewDidLoad(a) {
super.viewDidLoad()
let post = Post()
post.trimed()
}
}
Copy the code
- Applications in SwiftUI.
// Customize the View
struct CustomView<Content: View> :View {
// Attribute wrapping defines the content
@ViewBuilder var content: () -> Content
var body: some View {
ScrollView(.horizontal) {
HStack(content: content)
.padding()
}
}
}
struct ContentView: View {
var body: some View {
CustomView {
ForEach(0 ..< 10) { _ in
Image(systemName: "heart")
Text("SwiftUI")}}}}Copy the code