jeudi 11 février 2021

How to handle multiple possible scenarios in Swift

So I'm making a card game with art on each card, and I have to use swift to take the data from the card and make it into art. Each card has 4 attributes and each attribute has 3 possibilities.

Before the card has the 4 attributes(color, shading, shape, numOfShapes) as properties, so I have them all ready to go.

I tried doing it with if loops.

if color = 1{
    if shape = 1{
        if shading = 1{
            if numOfShapes = 1 {
              Rectangle().fill(color1).opacity(0.5)
            }
            if numOfShapes = 2 {
              Rectangle().fill(color1).opacity(0.5)
              Rectangle().fill(color1).opacity(0.5)
            }

        }
        if shading = 2{
        
        }
        if shading = 3{
            
        }
    }
    if shape = 2
    if shape = 3
}


and this did not work because there are too many possible combinations for swift to compile. So then I tried making dictionaries. These would have the referring numbers as keys and the values would be the required code in the for of a string

var symboler = [
        1: "Rectangle()",
        2: "Circle()",
        3: "RoundedRectangle(cornerRadius: 12)"
        
    ]

But I was not able to find a method to turn a string back into executable code.

What is the solution to this issue?

Aucun commentaire:

Enregistrer un commentaire