Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 检查多个布尔并根据该布尔更改图像的最快方法_Swift_Loops - Fatal编程技术网

Swift 检查多个布尔并根据该布尔更改图像的最快方法

Swift 检查多个布尔并根据该布尔更改图像的最快方法,swift,loops,Swift,Loops,我有多个成就,用户可以在游戏中实现。当用户进入成就VC时,他应该看到成就完成时的图像 我现在在viewDidLoad函数中使用以下代码: if unlockedAchievement1 == true { achievement1Text.textColor = UIColor.greenColor() achievement1Image.image = UIImage(named: "achievement1Unlocked")

我有多个成就,用户可以在游戏中实现。当用户进入成就VC时,他应该看到成就完成时的图像

我现在在viewDidLoad函数中使用以下代码:

if unlockedAchievement1 == true
        {
            achievement1Text.textColor = UIColor.greenColor()
            achievement1Image.image = UIImage(named: "achievement1Unlocked")
        }
        if unlockedAchievement2 == true
        {
            achievement2Text.textColor = UIColor.greenColor()
            achievement2Image.image = UIImage(named: "achievement2Unlocked")
        }

这会起作用,但是复制粘贴这个超时需要很多时间。我怎样才能缩短这个?我应该上课吗?我读过关于循环的文章,但我不太明白。欢迎任何帮助

我觉得我以前见过这种情况。但无论如何

//use array of bools. First declare as empty array.
//Use Core data to achieve saving the Booleans if you need to. 
let achievements:[Bool] = [] 
let achievementsImage:[UIImage] = []
let achievementsText:[UITextView] = []

func viewDidLoad() {
    //Say you have 5 achievements.
    for index in 0 ..< 5 {
        achievements.append(false)
        achievementsImage.append(UIImage(named: "achievementLocked"))
        achievementText.append(UITextView())
        //Then you can edit it, such as.
        achievementText[index].frame = CGRect(0, 0, 100, 100)
        achievementText[index].text = "AwesomeSauce"
        self.view.addSubview(achievementText[index])
        achievementText[index].textColor = UIColor.blackColor()
    }
}

//Call this function and it will run through your achievements and determine what needs to be changed
func someFunction() {
    for index in 0 ..< achievements.count {
        if(achievements[index]) {
            achievements[index] = true
            achievementsImage[index] = UIImage(named: String(format: "achievement%iUnlocked", index))
            achievementsText[index].textColor = UIColor.greenColor
        }
    }
}
//使用布尔数组。首先声明为空数组。
//如果需要,可以使用核心数据保存布尔值。
让成就:[Bool]=[]
让achievementsImage:[UIImage]=[]
让achievementsText:[UITextView]=[]
func viewDidLoad(){
//假设你有5项成就。
对于0..<5中的索引{
成就。追加(false)
append(UIImage(名为:“achievementLocked”))
append(UITextView())
//然后可以对其进行编辑,例如。
achievementText[index].frame=CGRect(0,0,100,100)
achievementText[index].text=“AwesomeSauce”
self.view.addSubview(achievementText[索引])
achievementText[index].textColor=UIColor.blackColor()
}
}
//调用此函数,它将贯穿您的成就并确定需要更改的内容
func someFunction(){
对于0中的索引..<成就数.count{
if(成绩[索引]){
成绩[索引]=正确
achievementsImage[索引]=UIImage(命名为:字符串(格式:“Achievent%iUnlocked”,索引))
achievementsText[index].textColor=UIColor.greenColor
}
}
}

我觉得我以前见过这种情况。但无论如何

//use array of bools. First declare as empty array.
//Use Core data to achieve saving the Booleans if you need to. 
let achievements:[Bool] = [] 
let achievementsImage:[UIImage] = []
let achievementsText:[UITextView] = []

func viewDidLoad() {
    //Say you have 5 achievements.
    for index in 0 ..< 5 {
        achievements.append(false)
        achievementsImage.append(UIImage(named: "achievementLocked"))
        achievementText.append(UITextView())
        //Then you can edit it, such as.
        achievementText[index].frame = CGRect(0, 0, 100, 100)
        achievementText[index].text = "AwesomeSauce"
        self.view.addSubview(achievementText[index])
        achievementText[index].textColor = UIColor.blackColor()
    }
}

//Call this function and it will run through your achievements and determine what needs to be changed
func someFunction() {
    for index in 0 ..< achievements.count {
        if(achievements[index]) {
            achievements[index] = true
            achievementsImage[index] = UIImage(named: String(format: "achievement%iUnlocked", index))
            achievementsText[index].textColor = UIColor.greenColor
        }
    }
}
//使用布尔数组。首先声明为空数组。
//如果需要,可以使用核心数据保存布尔值。
让成就:[Bool]=[]
让achievementsImage:[UIImage]=[]
让achievementsText:[UITextView]=[]
func viewDidLoad(){
//假设你有5项成就。
对于0..<5中的索引{
成就。追加(false)
append(UIImage(名为:“achievementLocked”))
append(UITextView())
//然后可以对其进行编辑,例如。
achievementText[index].frame=CGRect(0,0,100,100)
achievementText[index].text=“AwesomeSauce”
self.view.addSubview(achievementText[索引])
achievementText[index].textColor=UIColor.blackColor()
}
}
//调用此函数,它将贯穿您的成就并确定需要更改的内容
func someFunction(){
对于0中的索引..<成就数.count{
if(成绩[索引]){
成绩[索引]=正确
achievementsImage[索引]=UIImage(命名为:字符串(格式:“Achievent%iUnlocked”,索引))
achievementsText[index].textColor=UIColor.greenColor
}
}
}

我建议将您的
unlockdacheement1
unlockdacheement2
等对象设置为元组,第一个值为原始Bool,第二个值为图像名称。您将使用以下代码:

// First create your data array
var achievements = [(Bool, String)]()
// Then you can make your achievements.
// Initially, they're all unearned, so the Bool is false.
let achievement1 = (false, "achievement1UnlockedImageName")
achievements.append(achievement1)
let achievement2 = (false, "achievement2UnlockedImageName")
achievements.append(achievement2)

// Now that you have your finished data array, you can use a simple for-loop to iterate.
// Create a new array to hold the images you'll want to show.
var imagesToShow = [String]()
for achievement in achievements {
    if achievement.0 == true {
        imagesToShow.append(achievement.1)
    }
}

我建议将您的
unlockdacheement1
unlockdacheement2
等对象设置为元组,第一个值为原始Bool,第二个值为图像名称。您将使用以下代码:

// First create your data array
var achievements = [(Bool, String)]()
// Then you can make your achievements.
// Initially, they're all unearned, so the Bool is false.
let achievement1 = (false, "achievement1UnlockedImageName")
achievements.append(achievement1)
let achievement2 = (false, "achievement2UnlockedImageName")
achievements.append(achievement2)

// Now that you have your finished data array, you can use a simple for-loop to iterate.
// Create a new array to hold the images you'll want to show.
var imagesToShow = [String]()
for achievement in achievements {
    if achievement.0 == true {
        imagesToShow.append(achievement.1)
    }
}

谢谢你的回答。然而,我首先得到一个错误:无法通过下标赋值:成就是一个let常量。我把它改成了一个变量。我首先像你说的那样声明它为一个空数组,然后尝试加载它,但是它崩溃了,因为数组没有索引。我在成就数组中添加了5次false,再试了一次,结果它崩溃了,因为“在展开可选值时意外地发现了nil”。断点设置为“achievementsImage[index]=UIImage(名为:“achievementLocked”)!”。我怎样才能摆脱这些错误呢?哈哈,我的错。我犯了愚蠢的错误。Ima现在编辑它,应该是无错误的。但是还没有测试。谢谢你的编辑。只剩下一件事,设置所有成就图像的最快方法是什么?现在我正在这样做:achievement1Image.image=achievementsImage[0]achievement2Image.image=achievementsImage[1]还有什么方法可以缩短它吗?非常感谢。你什么意思?您能详细说明或显示代码吗?谢谢您的回答。然而,我首先得到一个错误:无法通过下标赋值:成就是一个let常量。我把它改成了一个变量。我首先像你说的那样声明它为一个空数组,然后尝试加载它,但是它崩溃了,因为数组没有索引。我在成就数组中添加了5次false,再试了一次,结果它崩溃了,因为“在展开可选值时意外地发现了nil”。断点设置为“achievementsImage[index]=UIImage(名为:“achievementLocked”)!”。我怎样才能摆脱这些错误呢?哈哈,我的错。我犯了愚蠢的错误。Ima现在编辑它,应该是无错误的。但是还没有测试。谢谢你的编辑。只剩下一件事,设置所有成就图像的最快方法是什么?现在我正在这样做:achievement1Image.image=achievementsImage[0]achievement2Image.image=achievementsImage[1]还有什么方法可以缩短它吗?非常感谢。你什么意思?你能详细说明或显示代码吗?如果我读对了,我正在创建一个数组,其中只包含要显示的图像,对吗?但是这怎么会自动改变形象呢?假设你有10项成就
imagesToShow.count
将是解锁的图像数,
10-imagesToShow.count
将是默认锁定图像的图像数。如果我读取正确,我将创建一个仅包含要显示图像的数组,对吗?但是这怎么会自动改变图像呢?假设你有10个achie