diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3b29812 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +.DS_Store +/.build +/Packages +/*.xcodeproj +xcuserdata/ +DerivedData/ +.swiftpm/config/registries.json +.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata +.netrc diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..1be71b8 --- /dev/null +++ b/Package.swift @@ -0,0 +1,30 @@ +// swift-tools-version: 5.6 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "SwiftUIViewTypeInspection", + platforms: [ + .iOS(.v13), + .macCatalyst(.v13), + .macOS(.v10_15), + .watchOS(.v6), + ], + products: [ + // Products define the executables and libraries a package produces, and make them visible to other packages. + .library( + name: "SwiftUIViewTypeInspection", + targets: ["SwiftUIViewTypeInspection"]), + ], + targets: [ + // Targets are the basic building blocks of a package. A target can define a module or a test suite. + // Targets can depend on other targets in this package, and on products in packages this package depends on. + .target( + name: "SwiftUIViewTypeInspection", + dependencies: []), + .testTarget( + name: "SwiftUIViewTypeInspectionTests", + dependencies: ["SwiftUIViewTypeInspection"]), + ] +) diff --git a/README.md b/README.md new file mode 100644 index 0000000..997beba --- /dev/null +++ b/README.md @@ -0,0 +1,48 @@ +# SwiftUIViewTypeInspection + +SwiftUIViewTypeInspection provides helpers methods on SwiftUI.View to inspect its real concrete +type. + +## View.inspectType() -> Self + +Print its real concrete type representation to the standard output stream. + +Example: + +```swift +import SwiftUIViewTypeInspection + +struct MyView: View { + var body: some View { + HStack { + Text("Hello, ") + Text("world!") + } + .inspectType() // prints "HStack>" + } +} +``` + +## View.inspectType(to output: inout Target) -> Self + +Store its real concrete type representation to the target stream. + +Tip: the `String` type conforms `TextOutputStream`. + +Example: + +```swift +import SwiftUIViewTypeInspection + +var output: String = "" + +struct MyView: View { + var body: some View { + HStack { + Text("Hello, ") + Text("world!") + } + .inspectType(to: &output) // stores "HStack>\n" + } +} +``` diff --git a/Sources/SwiftUIViewTypeInspection/SwiftUIViewTypeInspection.swift b/Sources/SwiftUIViewTypeInspection/SwiftUIViewTypeInspection.swift new file mode 100644 index 0000000..60d47e3 --- /dev/null +++ b/Sources/SwiftUIViewTypeInspection/SwiftUIViewTypeInspection.swift @@ -0,0 +1,53 @@ +import SwiftUI + +extension View { + /// Print its real concrete type representation to the standard output stream. + /// + /// Example: + /// + /// ```swift + /// import SwiftUIViewTypeInspection + /// + /// struct MyView: View { + /// var body: some View { + /// HStack { + /// Text("Hello, ") + /// Text("world!") + /// } + /// .inspectType() // prints "HStack>" + /// } + /// } + /// ``` + public func inspectType() -> Self { + var output: String = "" + _ = self.inspectType(to: &output) + print(output) + return self + } + + /// Store its real concrete type representation to the target stream. + /// + /// Tip: the `String` type conforms `TextOutputStream`. + /// + /// Example: + /// + /// ```swift + /// import SwiftUIViewTypeInspection + /// + /// var output: String = "" + /// + /// struct MyView: View { + /// var body: some View { + /// HStack { + /// Text("Hello, ") + /// Text("world!") + /// } + /// .inspectType(to: &output) // stores "HStack>\n" + /// } + /// } + /// ``` + public func inspectType(to output: inout Target) -> Self { + print(Mirror(reflecting: self).subjectType, to: &output) + return self + } +} diff --git a/Tests/SwiftUIViewTypeInspectionTests/SwiftUIViewTypeInspectionTests.swift b/Tests/SwiftUIViewTypeInspectionTests/SwiftUIViewTypeInspectionTests.swift new file mode 100644 index 0000000..a711e51 --- /dev/null +++ b/Tests/SwiftUIViewTypeInspectionTests/SwiftUIViewTypeInspectionTests.swift @@ -0,0 +1,21 @@ +import XCTest +import SwiftUI + +@testable import SwiftUIViewTypeInspection + +final class SwiftUIViewTypeInspectionTests: XCTestCase { + func test_insepctType() throws { + var output: String = "" + _ = FixtureView().body.inspectType(to: &output) + XCTAssertEqual(output, "HStack>\n") + } +} + +private struct FixtureView: View { + var body: some View { + HStack { + Text("Hello, ") + Text("world!") + } + } +}