-3

I have the following chunk of code in a SwiftUI app:

struct BtnTxtView: View {
    var theLabel:String
    var highLight: Bool

    var body: some View {
        let crnrRad:CGFloat = 5.0
        Text(theLabel)
            .foregroundColor(.blue)
            .padding(.vertical)
            .font(.largeTitle)
            .if (highLight) { $0
                .cornerRadius(crnrRad)
                .overlay(RoundedRectangle(cornerRadius: crnrRad)
                            .stroke(Color.purple, lineWidth: 4.0))
            }
    }
}

I get this error message:

Value of type 'some View' has no member 'if'

on the line:

        .if (highLight) { $0

Having used this type of syntax in another project without any issue I did this test:

I copied the chunk of code above and put it in my other project and tried to compile.

I get absolutely no error. Why is that?

Can anyone think of a possible reason, why I see an error in one case and not the other?

Michel
  • 10,303
  • 17
  • 82
  • 179
  • 4
    In the project where it *does* work, you have an extension to `View` included somewhere called `if` -- probably something like this -- https://stackoverflow.com/questions/57467353/conditional-property-in-swiftui If you do not include that extension, it won't work, as it's not a part of the vanilla SwiftUI – jnpdx Aug 22 '21 at 03:23
  • Beside your question or the answer! why this `.if()` modifier? you can use just `.overlay` and use condition there as well. No need for `.if()` – ios coder Aug 22 '21 at 03:36
  • @jnpdx; yes I checked and you are right. – Michel Aug 22 '21 at 03:48
  • @swiftPunk; OK, I need to look precisely how to use overlay for that then. – Michel Aug 22 '21 at 03:49
  • @Michel: absolutely you do not need that modifier, it is just a fancy way of typing code, look my answer. – ios coder Aug 22 '21 at 03:58

1 Answers1

2

Here the code that you need to use, there is no need to that .if() modifier:

struct BtnTxtView: View {

    var theLabel:String
    var highLight: Bool

    var body: some View {
        
        let crnrRad:CGFloat = 5.0
        
        Text(theLabel)
            .foregroundColor(.blue)
            .padding(.vertical)
            .font(.largeTitle)
            .cornerRadius(highLight ? crnrRad : 0.0)
            .overlay(highLight ? RoundedRectangle(cornerRadius: crnrRad)
                .stroke(Color.purple, lineWidth: 4.0) : nil)
    }
}
ios coder
  • 1
  • 4
  • 31
  • 91
  • I gave a quick try to overlay that you suggest. It may be that I need to use it more, but I have the feeling the .if option gives more flexibility. I may be wrong. Example in this demo from another post of mine: https://github.com/zaxonus/SUI_BtnSwt.git – Michel Aug 24 '21 at 02:13
  • It is not true what you are thinking, `.overlay()` is much better and easier and basic. You can use that custom modifier called `.if()` if you know the deference between them and could be more helpful in some case, but at least you have to know both. – ios coder Aug 24 '21 at 02:18
  • OK. Right. I need to use it a bit more then. Thanks. – Michel Aug 24 '21 at 02:30
  • As a matter of fact you may have some interesting comment on this subject, if you look at the demo in my post https://stackoverflow.com/questions/68871696/button-handling-in-swiftui. – Michel Aug 24 '21 at 03:25