-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.DS_Store | ||
/.build | ||
/Packages | ||
/*.xcodeproj | ||
xcuserdata/ | ||
DerivedData/ | ||
.swiftpm/config/registries.json | ||
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata | ||
.netrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"]), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<TupleView<(Text, Text)>>" | ||
} | ||
} | ||
``` | ||
|
||
## View.inspectType<Target: TextOutputStream>(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<TupleView<(Text, Text)>>\n" | ||
} | ||
} | ||
``` |
53 changes: 53 additions & 0 deletions
53
Sources/SwiftUIViewTypeInspection/SwiftUIViewTypeInspection.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<TupleView<(Text, Text)>>" | ||
/// } | ||
/// } | ||
/// ``` | ||
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<TupleView<(Text, Text)>>\n" | ||
/// } | ||
/// } | ||
/// ``` | ||
public func inspectType<Target: TextOutputStream>(to output: inout Target) -> Self { | ||
print(Mirror(reflecting: self).subjectType, to: &output) | ||
return self | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
Tests/SwiftUIViewTypeInspectionTests/SwiftUIViewTypeInspectionTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<TupleView<(Text, Text)>>\n") | ||
} | ||
} | ||
|
||
private struct FixtureView: View { | ||
var body: some View { | ||
HStack { | ||
Text("Hello, ") | ||
Text("world!") | ||
} | ||
} | ||
} |