I have a simple View called TextView, the type of TextView is naturally View, I want make a modification on TextView like .foregroundColor(Color.red) and replace it again as itself! from my understanding my TextView is type View and should just working because the type is not Text But also I understand the complaining from SwiftUI which says cannot assign value of some View to type View. I like make some correction for solving issue, also I am not interested to add another initializer as foregroundColor.
import SwiftUI
struct ContentView: View {
@State private var myTextView: TextView?
var body: some View {
if let unwrappedMyTextView = myTextView {
unwrappedMyTextView
}
Button ("update") {
myTextView = TextView(stringOfText: "Hello, world!")//.foregroundColor(Color.red)
}
.padding()
}
}
struct TextView: View {
let stringOfText: String
init(stringOfText: String) {
self.stringOfText = stringOfText
}
var body: some View {
return Text(stringOfText)
}
}