2021年3月20日星期六

How can I make body without inheriting from other defined View in SwiftUI, How apple done this at first place?

I like to study and learn about creation of View without using inheriting, for example for showing my goal, I made a new type View called Color2, it works as expected, but in the fact it inheriting from well defined Color, So if I want make Color2 stand alone and not inheriting from anything I must create or return body, otherwise Color2 would not be compiles by SwiftUI! It is more like the old question of Which Came First, the Chicken or the Egg? How could Apple make the Color stand alone at first place?

So the question/problem is how can I return body without having a reference/defined View that conforms to View ? (See! I want create Egg without having Chicken)

This down code are working and they are JUST pseudocode:

struct ContentView: View {        var body: some View {                    Color2.red                }  }  

enum Color2 { case red, black }  

extension Color2: View {        public var body: some View {            switch self {                    case Color2.red: Color.red  // <<: Here how can I return a View if I want not using defined View!           case Color2.black: Color.black             }      }    }  

https://stackoverflow.com/questions/66728000/how-can-i-make-body-without-inheriting-from-other-defined-view-in-swiftui-how-a March 21, 2021 at 09:17AM

没有评论:

发表评论