How can I shorten this code using a structure?

I just started to study Structures, I think that this can be solved using Structures, but I'm not sure.

Case: I have a VC with many achievements that a player can unlock. Each achievement has: 1. an image of the border around the achievements of the image and text 2. the image of the achievement (each achievement has a simple “lock” to indicate that the achievement is blocked) 3. the text of the achievement 4. a logical value to determine the status of the achievement (false = blocked, true = unlocked) During the game, some achievements can be achieved. Thus, the boolean value will be true. When a player returns to a VC achievement, the achievement image must be set to the correct image for that achievement.

Problem: I now have the code:

func SetImagesForPowerUps()
    {
        if UnlockedAchievement1 == true
        {
            Achievement1Text.textColor = UIColor.greenColor()
            Achievement1Image.image = UIImage(named: "Achievement1Unlocked")
        }
        if UnlockedAchievement2 == true
        {
            Achievement2Text.textColor = UIColor.greenColor()
            Achievement2Image.image = UIImage(named: "Achievement2Unlocked")
        }
    }

, , . , , , , , .

: , ?

+4
1

// ?

var achievements:[Bool] = []
var achievementText:[UILabel] = [] //I am not sure what achievementText Parent class is
var achievementImage:[UIImage] = [] //AchievementImage is subclass of UIImage? Not sure what achievementImage parent class is. 

for index in 0 ..< x 
    //Where x is the number of possible achievements, default all achievements to false at the very beginning of the app or wherever.
    achievements.append(false)
}

func SetImagesForPowerUps() {
    for index in 0 ..< achievements.count { 
        if(achievements[index]) {
            achievementText[index].textColor = UIColor.greenColor()
            achievementImage[index].image = UIImage(named: "AchievementUnlocked")
        }
    }
}

, , , . achievements[level+1] = true //(If you use levels starting at 1)

+2

Source: https://habr.com/ru/post/1648964/


All Articles