0

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)
        
    }
 
}

enter image description here

ios coder
  • 1
  • 4
  • 31
  • 91
  • why not use it like this? ```if let unwrappedMyTextView = myTextView { unwrappedMyTextView.foregroundColor(Color.red) }``` – Raja Kishan Feb 23 '21 at 13:31
  • thanks, yes there is lots of ways, my point is replacing or updating, not exactly given red foregroundColor – ios coder Feb 23 '21 at 13:33
  • 1
    Then you can add a condition in the color function. it's not a good way to replace whole view with @State. – Raja Kishan Feb 23 '21 at 13:35

1 Answers1

0

The problem is that foregroundColor doesn't return TextView:

@inlinable public func foregroundColor(_ color: Color?) -> some View

Which means that

myTextView = TextView(stringOfText: "Hello, world!").foregroundColor(Color.red)

is no longer a TextView but some other View which is a result of applying foregroundColor.

Also, you shouldn't hold View properties as @State.

Try this instead:

struct ContentView: View {
    
    var body: some View {
        myTextView
    }
    
    var myTextView: some View {
        TextView(stringOfText: "Hello, world!")
            .foregroundColor(Color.red)
    }
}
pawello2222
  • 46,897
  • 22
  • 145
  • 209
  • Yes, exactly you pointed the right thing **func foregroundColor** returns **some View** with your code modification happening inside myTextView, which I would like it happens in **body**, my all point is replacing **TextView** with **some View** – ios coder Feb 23 '21 at 13:42
  • @swiftPunk *my all point is replacing TextView with some View* - then you can use `var myTextView: some View` as proposed above. Or is there anything else I'm missing? – pawello2222 Feb 23 '21 at 18:43
  • so you are using **TextView** to make **myTextView**, which I think you are transforming **View** to **some View** right? that is the way you found, and I have nothing against it, unless as you can see my original question was about making some kind of correction to my code that **TextView** type became **some View**, and after that correction I could easily modify a value of type **TextView** say: giving **foregroundColor** and then assign again to the same value which you can see in my code. I mean updating the Value type TextView that is what I am trying learn. – ios coder Feb 23 '21 at 21:15
  • @swiftPunk We don't transform anything here - that's the point. Any struct that conforms to View is already *some View* - whether you want it or not :) First I suggest you read [Opaque Types](https://docs.swift.org/swift-book/LanguageGuide/OpaqueTypes.html) and [What is the `some` keyword in Swift(UI)?](https://stackoverflow.com/q/56433665/8697793). – pawello2222 Feb 23 '21 at 21:25
  • I was thinking the same that Any struct that conforms to View is already some View, and I got xcode error of **Cannot assign value of type`some View to type TextView`** because TextView has View type and as you said conforms to some View, then It should just working, but did not worked, I already explained in my question about View and some View. I think for SwiftUI View and some View are different things – ios coder Feb 24 '21 at 00:18
  • @swiftPunk But `some View` is not a usual type, it's an opaque type. It behaves differently. Again, please read the links I posted before, they're the key to understand the difference between some View and View. – pawello2222 Feb 24 '21 at 00:47