What’s new in swift 5.5

Time:2021-9-27

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 has brought a lot of new content. The biggest update is the introduction of a new concurrent programming method.

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

Conditional compilation supports expressions

Swiftui will use the condition modifier when cross platform. The previous solution was to write a set of judgment system. After swift 5.5, the native supports conditional compilation expressions, which is more convenient for 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 // the result is a double type. Copy the code

The following code will report an error before swift 5.5 becausescaleIt is a double type, and the 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)
        }
    }
}

Static member lookup is supported 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()) // previous writing method
                . textfieldstyle (. Roundedborder) // new and 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 attribute wrapping

  • Attribute wrapping was introduced in swift 5.1.
  • Swift 5.4 supports wrapping attributes into local variables.
  • Swift 5.5 supports wrapping attributes to functions 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 like structures and classes.

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

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

//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"
    }
  }
]
"""

//Turn enumeration
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

② IOS underlying technology

③ IOS reverse protection

④ IOS interview collection

⑤ Large factory interview questions + bottom technology + reverse Security + swift

Remember to like your little partner~

Collecting is equal to whoring for nothing, and praising is the truth (´ ᴗღ) ღ

Recommended Today

Ffmpeg plays RTSP / webcam stream

This article will introduce how ffmpeg plays RTSP / webcam / file streams. The process is as follows: RTSP/Webcam/File > FFmpeg open and decode to BGR/YUV > OpenCV/OpenGL display code:https://github.com/ikuokuo/rt…, sub module RTSP local player Ffmpeg preparation git clone https://github.com/ikuokuo/rtsp-wasm-player.git cd rtsp-wasm-player export MY_ROOT=`pwd` # ffmpeg: https://ffmpeg.org/ git clone –depth 1 -b n4.4 https://git.ffmpeg.org/ffmpeg.git $MY_ROOT/3rdparty/source/ffmpeg […]