1

I try to write View extension by restrict where clause,code is very simple:

struct FooView : View {
 var body: some View {
 VStack {
 Text("hello world")
 }
 }
}
// only FooView type can invoke standard()
extension View where Self == FooView {
 func standard() -> some View {
 self
 .frame(width: 200, height: 50)
 }
}

I call standard() like this:

struct ContentView: View {
 
 var body: some View {
 
 VStack {
 FooView()
 .standard()
 }
 }
}

But run it will crashed in Xcode Preview! (Xcode 12.2 + MacOS 10.15.7) Is there something wrong with what I wrote? Thanks ;)

asked Oct 7, 2021 at 8:19
4
  • Why don't you just extend FooView instead of View? Commented Oct 7, 2021 at 9:03
  • @DávidPásztor you're right!but I just want to know extend from View why not working. ;) Commented Oct 7, 2021 at 11:30
  • @hopy any luck with this? am curious to find out as well. I wonder if its because is not a protocol. as the error says something about protocol witness etc.. Commented Jun 9, 2022 at 5:02
  • @Adam I'm not sure... Maybe I should try it in the latest Xcode 14 ;) Commented Jun 16, 2022 at 12:27

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.