Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to log in SwiftUI #604

Open
onmyway133 opened this issue Feb 14, 2020 · 0 comments
Open

How to log in SwiftUI #604

onmyway133 opened this issue Feb 14, 2020 · 0 comments
Labels

Comments

@onmyway133
Copy link
Owner

onmyway133 commented Feb 14, 2020

I see that the modifier needs to do something on the content, otherwise it is not getting called!
This logs on the modifier, when the View is created. A View won't be recreated unless necessary

struct LogModifier: ViewModifier {
    let text: String
    func body(content: Content) -> some View {
        print(text)
        return content
            .onAppear {}
    }
}

extension View {
    func log(_ text: String) -> some View {
        self.modifier(LogModifier(text: text))
    }
}
VStack {
    Text("")
        .log("a text")
}

Another simpler way is to make an extension

extension View {
    func log(_ any: Any) -> Self {
        print("\(any)")
        return self
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant