Swift 5.5 is built into Xcode 13, and although the version number is only 0.1, it may seem like a small upgrade, but it brings a lot of new content, the biggest of which is the introduction of a new way of concurrent programming.

This paper included: www.cocoachina.com/articles/90…

Conditional compilation supports expressions

SwiftUI will use conditional Modifier when cross-platform, the previous solution was to write a set of judgment system, Swift 5.5, native support for conditional compilation of expressions, more convenient cross-platform.

struct ContentView: View {
    var body: some View {
        Text("SwiftUI")
        #if os(iOS) 
            .foregroundColor(.blue)
        #elseif os(macOS)
            .foregroundColor(.green)
        #else
            .foregroundColor(.pink)
        #endif
    }
}
Copy the code

CGFloat and Double support implicit conversions

Let number1: CGFloat = 12.34 let number2: Double = 56.78 Let result = number1 + number2 // Result is a Double copy codeCopy the code

The following code will report an error before Swift 5.5 because scale is Double and CGFloat needs to be bound in SwiftUI.

Struct ContentView: View {@state private var scale = 1.0 // Double var body: some View {VStack {Image(systemName: ScaleEffect (scale) // Implicitly converts to CGFloat Slider(value: $scale, in: 0... 1)}}}Copy the code

Support for static Member Lookup in generic context

This new feature makes some of the syntax in SwiftUI simpler and easier to use.

struct ContentView: View { @Binding var name: String var body: some View { HStack { Text(name) TextField("", text: $name). / / textFieldStyle (RoundedBorderTextFieldStyle ()) / / before writing. TextFieldStyle (. RoundedBorder) / / new writing, more concise}}}Copy the code

Local variables support lazy

Func lazyInLocalContext() {print(" before lazy ") lazy var swift = "Hello swift 5.5" print(" after lazy ") print(swift)} // call LazyInLocalContext () /* Hello Swift 5.5 */ before lazy and after lazyCopy the code

Function and closure arguments support attribute wrapping

  • Attribute wrapping was introduced in Swift 5.1.
  • Swift 5.4 supports attribute wrapping to local variables.
  • Swift 5.5 supports attribute wrapping to function and closure parameters.
@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(@Trimmed content: String) {// Function arguments support PropertyWrapper print(content)}} let post = POST () post.trimed(content: "Swift 5.5 Property Wrappers ")Copy the code

Enumerations with associated values are supported for Codable

With this capability, enumerations can be used as data models, just like structures and classes.

  • Enumeration to JSON.
Count Score: Codable {case number(Score: Double) case letter(Score: String)} [Score] = [. Number (Score: 98.5),.letter(Score: 98.5) } // convert JSON let encoder = JSONEncoder() encoder. OutputFormatting =.prettyprinted do {let result = try encoder.encode(scores) let json = String(decoding: result, as: UTF8.self) print(json) } catch { print(error.localizedDescription) }Copy the code
  • JSON to enumeration.
enum Score: Codable { case number(score: Double) case letter(score: String) } // JSON let json = """ [ { "number" : {"score" : 98.5}, {"letter" : {"score" : }}] "" // let decoder = JSONDecoder() do {let scores = try decoder. Decode ([Score]. Self, from: json.data(using: .utf8)!) for score in scores { switch score { case let .number(value): print(value) case let .letter(value): print(value) } } } catch { print(error.localizedDescription) }Copy the code

The end of the article recommended: iOS popular anthology & video analysis

1) Swift

②iOS underlying technology

③iOS reverse protection

④iOS interview Collection

Big factory interview questions + underlying technology + reverse security +Swift

If you like it, please give it a thumbs up

Collection is equal to whoring, praise is true ღ(´ · ᴗ · ‘)ღ