New features of swift 5.5

Time:2021-7-23

Swift 5.5 is built into Xcode 13. Although the version number is only increased by 0.1, which seems to be a small version upgrade, it brings a lot of new content, among which the biggest update is the introduction of a new way of concurrent programming.

Included in this article:http://www.cocoachina.com/art…

Conditional compilation supports expressions

Swiftui will use conditional modifier when it is cross platform. The previous solution is to write a judgment system. After swift5.5, conditional compilation expressions are natively supported, which makes it more convenient to 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
    }
}

Cgfloat and double support implicit conversion

let number1: CGFloat = 12.34
let number2: Double = 56.78
Let result = Number1 + number2 // result copies the code for the double type

The following code will report an error before swift 5.5, becausescaleIt is a double type, and cgfloat type needs to be bound in swiftui.

struct ContentView: View {
    @State private var scale = 1.0 // double type

    var body: some View {
        VStack {
            Image(systemName: "heart")
                . scaleeffect (scale) // implicitly converted to cgfloat

            Slider(value: $scale, in: 0 ... 1)
        }
    }
}

Support static member lookup in generic context

This new feature makes some of the syntax in swiftui more concise and easy to use.

struct ContentView: View {
    @Binding var name: String

    var body: some View {
        HStack {
            Text(name)

            TextField("", text: $name)
                //. textfieldstyle (roundedbordertextfieldstyle()) // previously written
                . textfieldstyle (. Roundedborder) // new writing, more concise
        }
    }
}

Local variables support lazy

func lazyInLocalContext() {
    Print ("before lazy")
    lazy var swift = "Hello Swift 5.5"
    Print ("after lazy")

    print(swift)
}

//Call
lazyInLocalContext()

/*Output
Before lazy
After lazy
Hello Swift 5.5
*/

Function and closure parameters support property wrapping

  • Swift 5.1 introduces attribute wrapping.
  • 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 trimmed (@ trimmed content: String) {// function parameters support propertywrapper
        print(content)
    }
}

let post = Post()
post.trimed(content: "  Swift 5.5 Property Wrappers  ")

Enumerations with associated values support codable

With this function, enumerations can be used as data models just like structs and classes.

  • Enumerate to JSON.
//Defines an enumeration with associated values
enum Score: Codable {
    case number(score: Double)
    case letter(score: String)
}

//Creating objects
Let scores: [score] = [. Number (score: 98.5),. Letter (score: "excellent")]

//Transfer to 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)
}
  • JSON to enumeration.
enum Score: Codable {
    case number(score: Double)
    case letter(score: String)
}

// JSON
let json = """
[
  {
    "number" : {
      "score" : 98.5
    }
  },
  {
    "letter" : {
      "Score": "excellent"
    }
  }
]
"""

//To enumerate
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)
}

Recommendation at the end of the article: IOS popular anthology & video analysis

① Swift

② Underlying technology of IOS

③ IOS reverse protection

④ IOS interview collection

⑤ Interview questions + bottom technology + reverse Security + swift

If you like, please like it~

Collection is equal to whoring, praise is the truth