user2635911
user2635911

Reputation: 502

iOS SwiftUI Function returning some view not showing up in UI

I am currently attempting assignment 3 of stanford's 193p 2020 course. I am having trouble in trying to get my function createCardContent to actually display the Shape object it should be displaying. Instead of displaying a shape object on the cards, it is not displaying anything, which ends up just showing empty cards (and I don't know why) image here.

If I remove the text @ViewBuilder, then I get an error

Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type

If I add the return inside the if-else if statements, I get an error saying

Function declares an opaque return type, but the return statements in its body do not have matching underlying types

struct CardView: View {
        var card: SetGameEngine.Card
    
    var body: some View {
        GeometryReader { geometry in
            self.testBody(for: geometry.size)
        }
    }
    
    func testBody(for size: CGSize) -> some View {
        ZStack {
            HStack {
                ForEach(0..<card.symbolCount) { _ in
                    self.createCardContent()
                }
                
            }
       } .cardify()
    }


    @ViewBuilder
    func createCardContent() -> some View {
        let colour = self.colour()
        let symbol = self.card.symbol
        let shade = self.card.shade
        
        if symbol == .oval, shade == .solid {
            Capsule().fill(colour)
        } else if symbol == .squiggle, shade == .solid {
            Rectangle().fill(colour)
        } else if symbol == .diamond, shade == .solid {
            Diamond().fill(colour)
        } else if symbol == .oval, shade == .empty {
            Capsule().stroke(colour)
        } else if symbol == .squiggle, shade == .empty {
            Rectangle().stroke(colour)
        } else if symbol == .diamond, shade == .empty {
            Diamond().stroke(colour)
        } else {
            Capsule().fill(Color.pink)
        }
            
    }

EDIT: This is very weird. When I remove the local variables I wrote in the function createCardContent and their references, the card content gets populated by the shapes I want. Here is my new function createCardContent. Can anyone explain to me why this is working but the previous function is not?

@ViewBuilder
func createCardContent() -> some View {
    
    if card.symbol == .oval && card.shade == .solid {
        Capsule().fill(colour())
    } else if card.symbol == .squiggle && card.shade  == .solid {
         return Rectangle().fill(colour())
    } else if card.symbol == .diamond && card.shade  == .solid {
         return Diamond().fill(colour())
    } else if card.symbol == .oval && card.shade  == .empty {
          return Capsule().stroke(colour())
    } else if card.symbol == .squiggle && card.shade  == .empty {
         return Rectangle().stroke(colour())
    } else if card.symbol == .diamond && card.shade == .empty {
         return Diamond().stroke(colour())
    } else {
        Diamond().stroke(colour())
    }
        
}

Upvotes: 0

Views: 233

Answers (0)

Related Questions