1

I have a simple View called TextView, I could make this View working without explicitly using some View like this code:

struct TextView: View {

    var body = Text("Hello, world!")
 
}

now I like to control the String also like this code:

struct TextView: View {

    let stringOfText: String
    
    init(stringOfText: String) {
        self.stringOfText = stringOfText
    }

    var body = Text(stringOfText)
 
}

with second code I get this Error:

enter image description here

How could I possibly solve this issue without using Binding or State wrappers?

2
  • 1
    Why would you want to do that? Commented Feb 23, 2021 at 13:20
  • 2
    I don't think it is a good way - what you're trying to do is kind of against common SwiftUI practices and may be hard to read for others. I suggest you use this for learning purposes only. The error occurs because body is no longer a computed property, thus, you can't initialise it with other properties outside init. Commented Feb 23, 2021 at 13:23

1 Answer 1

1

I have doubts all this is good SwiftUI code style, but following your first part you can use

struct TextView: View {
    var body: Text

    init(stringOfText: String) {
        body = Text(stringOfText)
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.