How do view types like Text or Image conform to the View protocol in SwiftUI?

前端 未结 4 1188
后悔当初
后悔当初 2021-02-20 08:39

There is one big thing that confuses me about view types in SwiftUI:

They don\'t seem to conform to the View protocol, but somehow, they mysterious

4条回答
  •  爱一瞬间的悲伤
    2021-02-20 08:42

    This question was asked on June 6, 2019, during WWDC, when we only had the first beta of Xcode 11 and SwiftUI. So answering this question correctly requires access to that version of SwiftUI. You can download Xcode 11 beta 1 here. (Thank you, xcodereleases.com!) You are in for an adventure trying to unpack the archive, though, because (I think) it was signed with a certificate that has since expired. I resorted to black magic (stepping through the xip command in LLDB and modifying memory at a key moment to subvert the certificate verification). You could perhaps just set your system time back to June 6, 2019 before unpacking.

    Anyway, here's the secret to understanding why Text didn't appear to conform to View: Xcode, and Apple's documentation generator, intentionally omit identifiers in the SDK that start with _.

    So if you want to see the full public declaration of a type, you cannot rely on Xcode or the documentation to show it to you. Instead, you have to dig up the .swiftinterface file for the module. For SwiftUI, you can find it here, relative to the Xcode.app directory:

    Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks/SwiftUI.framework/Modules/SwiftUI.swiftmodule/arm64.swiftinterface
    

    In the Xcode 11 beta 1 version of that file, you won't find a direct conformance Text: View. Instead, you will find this:

    @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
    extension Text : _UnaryView {
      public static func _makeView(view: _GraphValue, inputs: _ViewInputs) -> _ViewOutputs
      public typealias Body = Swift.Never
    }
    

    And you will find that _UnaryView is a sub-protocol of View:

    @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
    public protocol _UnaryView : SwiftUI.View where Self.Body : SwiftUI._UnaryView {
    }
    

    So, in Xcode 11 beta 1 and the corresponding iOS, macOS, tvOS, and watchOS betas, Text conforms to View indirectly, through its conformance to _UnaryView. Since _UnaryView is part of the SDK and begins with _, Xcode and the Apple documentation hide that symbol. So you can't see the conformance through normal methods.

    At some later point (but still, I believe, during the Xcode 11.0 beta period), Apple eliminated the _UnaryView protocol and made Text conform directly to View. So if you check the SwiftUI .swiftinterface file in Xcode 11.4 (the current version as I write this), you'll find this:

    @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
    extension Text : SwiftUI.View {
      public static func _makeView(view: SwiftUI._GraphValue, inputs: SwiftUI._ViewInputs) -> SwiftUI._ViewOutputs
      public typealias Body = Swift.Never
    }
    

提交回复
热议问题