I would like to use this same code on multiple pages of photos (to help my son recognize people). So instead of reinserting the name of each person, I created the appearingInPhoto
(tuple or array), so I can utilize just .a .b .c, instead of having to go and rename each line of code.
Is there a way to simplify the code further, so I can just have code that checks if a button is pressed (check that sender.currentTitle
is equal to any of the tuple values) and if yes, run this code?
print("pressed \(appearingInPhoto.a)")
label.text = appearingInPhoto.a
soundName = appearingInPhoto.a
This is the full function now:
var appearingInPhoto = (a:"omar", b:"john", c:"thomas")
@IBAction func buttonPressed(_ sender: UIButton) {
var soundName: String? = nil
if sender.currentTitle == appearingInPhoto.a {
print("pressed \(appearingInPhoto.a)")
label.text = appearingInPhoto.a
soundName = appearingInPhoto.a
}else if sender.currentTitle == appearingInPhoto.b {
print("pressed \(appearingInPhoto.b)")
label.text = appearingInPhoto.b
soundName = appearingInPhoto.b
}else if sender.currentTitle == appearingInPhoto.c {
print("pressed \(appearingInPhoto.c)")
label.text = appearingInPhoto.c
soundName = appearingInPhoto.c
}
if let soundName = soundName {
playSoundFile(soundName)
}
}
var appearingInPhoto:[String] = ["omar","john","thomas"]
Then loop over it \$\endgroup\$