Created
January 7, 2020 16:28
-
-
Save DevAndArtist/6aff9cd6669ab39dbd7383a414ba8efc to your computer and use it in GitHub Desktop.
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
import SwiftUI | |
class Base: CustomDebugStringConvertible { | |
var debugDescription: String { | |
"\(type(of: self)) \(ObjectIdentifier(self))" | |
} | |
init() { | |
print("🟢 init\t", self) | |
} | |
deinit { | |
print("🔴 deinit", self) | |
} | |
} | |
final class A: Base {} | |
final class B: Base {} | |
final class C: Base {} | |
enum E { | |
enum Kind: Equatable { | |
case a | |
case b | |
case c | |
var next: Kind { | |
switch self { | |
case .a: | |
return .b | |
case .b: | |
return .c | |
case .c: | |
return .a | |
} | |
} | |
} | |
case a(A) | |
case b(B) | |
case c(C) | |
var kind: Kind { | |
switch self { | |
case .a: | |
return .a | |
case .b: | |
return .b | |
case .c: | |
return .c | |
} | |
} | |
var base: Base { | |
switch self { | |
case .a(let a): | |
return a | |
case .b(let b): | |
return b | |
case .c(let c): | |
return c | |
} | |
} | |
} | |
struct Parent: View { | |
@State | |
private var _kind: E.Kind = .a | |
private func _initE() -> E { | |
switch _kind { | |
case .a: | |
return .a(A()) | |
case .b: | |
return .b(B()) | |
case .c: | |
return .c(C()) | |
} | |
} | |
var body: some View { | |
VStack { | |
Child(_initE()) | |
Button("Inject next E:") { | |
print("\n🔸 Button action:\n") | |
self._kind = self._kind.next | |
} | |
} | |
} | |
} | |
struct Child: View { | |
@State | |
private var _e: E | |
init(_ e: E) { | |
self.__e = State(wrappedValue: e) | |
print("Child init call:") | |
dump(__e) | |
} | |
var body: some View { | |
print("Child body call:") | |
dump(__e) | |
return Text(_e.base.debugDescription) | |
} | |
} | |
struct TestCase_PreviewProvider: PreviewProvider { | |
static var previews: some View { | |
Parent() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment