What is the `some` keyword in Swift(UI)?

后端 未结 11 660
庸人自扰
庸人自扰 2020-12-04 04:54

The new SwiftUI tutorial has the following code:

struct ContentView: View {
    var body: some View {
        Text(         


        
11条回答
  •  孤街浪徒
    2020-12-04 05:45

    'some' means opaque type. In SwiftUI, View is declared as a protocol

    @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
    public protocol View {
    
        /// The type of view representing the body of this view.
        ///
        /// When you create a custom view, Swift infers this type from your
        /// implementation of the required `body` property.
        associatedtype Body : View
    
        /// Declares the content and behavior of this view.
        var body: Self.Body { get }
    }
    

    When you create your view as Struct, you conform to the View protocol and tell that the var body will return something which will be confirming to View Protocol. Its like a generic Protocol abstraction where you don't have to Define the concrete Type.

提交回复
热议问题