Emit different text and sound when each of three buttons is pressed

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












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)








share|improve this question














bumped to the homepage by Community♦ 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.














  • You're using the wrong data type. You want an array, not a dictionary. var appearingInPhoto:[String] = ["omar","john","thomas"]Then loop over it
    – Snowbody
    Jan 12 at 22:36







  • 1




    I feel that you need a model to save all information related to the user that you will show. Also detecting the appearing person by comparing the text of the button is not considered safe. if you can describe more what you want top achieve or a screenshot of the screen that you have implanted to understand the context then we can help to improve the implementation
    – iOSGeek
    Jan 16 at 20:30
















up vote
0
down vote

favorite












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)








share|improve this question














bumped to the homepage by Community♦ 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.














  • You're using the wrong data type. You want an array, not a dictionary. var appearingInPhoto:[String] = ["omar","john","thomas"]Then loop over it
    – Snowbody
    Jan 12 at 22:36







  • 1




    I feel that you need a model to save all information related to the user that you will show. Also detecting the appearing person by comparing the text of the button is not considered safe. if you can describe more what you want top achieve or a screenshot of the screen that you have implanted to understand the context then we can help to improve the implementation
    – iOSGeek
    Jan 16 at 20:30












up vote
0
down vote

favorite









up vote
0
down vote

favorite











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)








share|improve this question













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)










share|improve this question












share|improve this question




share|improve this question








edited Jan 13 at 1:12









200_success

123k14143401




123k14143401









asked Jan 12 at 22:31









mikeT

61




61





bumped to the homepage by Community♦ 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community♦ 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.













  • You're using the wrong data type. You want an array, not a dictionary. var appearingInPhoto:[String] = ["omar","john","thomas"]Then loop over it
    – Snowbody
    Jan 12 at 22:36







  • 1




    I feel that you need a model to save all information related to the user that you will show. Also detecting the appearing person by comparing the text of the button is not considered safe. if you can describe more what you want top achieve or a screenshot of the screen that you have implanted to understand the context then we can help to improve the implementation
    – iOSGeek
    Jan 16 at 20:30
















  • You're using the wrong data type. You want an array, not a dictionary. var appearingInPhoto:[String] = ["omar","john","thomas"]Then loop over it
    – Snowbody
    Jan 12 at 22:36







  • 1




    I feel that you need a model to save all information related to the user that you will show. Also detecting the appearing person by comparing the text of the button is not considered safe. if you can describe more what you want top achieve or a screenshot of the screen that you have implanted to understand the context then we can help to improve the implementation
    – iOSGeek
    Jan 16 at 20:30















You're using the wrong data type. You want an array, not a dictionary. var appearingInPhoto:[String] = ["omar","john","thomas"]Then loop over it
– Snowbody
Jan 12 at 22:36





You're using the wrong data type. You want an array, not a dictionary. var appearingInPhoto:[String] = ["omar","john","thomas"]Then loop over it
– Snowbody
Jan 12 at 22:36





1




1




I feel that you need a model to save all information related to the user that you will show. Also detecting the appearing person by comparing the text of the button is not considered safe. if you can describe more what you want top achieve or a screenshot of the screen that you have implanted to understand the context then we can help to improve the implementation
– iOSGeek
Jan 16 at 20:30




I feel that you need a model to save all information related to the user that you will show. Also detecting the appearing person by comparing the text of the button is not considered safe. if you can describe more what you want top achieve or a screenshot of the screen that you have implanted to understand the context then we can help to improve the implementation
– iOSGeek
Jan 16 at 20:30










1 Answer
1






active

oldest

votes

















up vote
0
down vote













Slightly modified (using array):



var appearingInPhoto = ["omar", "john", "thomas"]

@IBAction func buttonPressed(_ sender: UIButton)

guard let soundName = sender.currentTitle else
return

if appearingInPhoto.contains(soundName)
print("pressed (soundName)")
label.text = soundName
playSoundFile(soundName)








share|improve this answer





















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184994%2femit-different-text-and-sound-when-each-of-three-buttons-is-pressed%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Slightly modified (using array):



    var appearingInPhoto = ["omar", "john", "thomas"]

    @IBAction func buttonPressed(_ sender: UIButton)

    guard let soundName = sender.currentTitle else
    return

    if appearingInPhoto.contains(soundName)
    print("pressed (soundName)")
    label.text = soundName
    playSoundFile(soundName)








    share|improve this answer

























      up vote
      0
      down vote













      Slightly modified (using array):



      var appearingInPhoto = ["omar", "john", "thomas"]

      @IBAction func buttonPressed(_ sender: UIButton)

      guard let soundName = sender.currentTitle else
      return

      if appearingInPhoto.contains(soundName)
      print("pressed (soundName)")
      label.text = soundName
      playSoundFile(soundName)








      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Slightly modified (using array):



        var appearingInPhoto = ["omar", "john", "thomas"]

        @IBAction func buttonPressed(_ sender: UIButton)

        guard let soundName = sender.currentTitle else
        return

        if appearingInPhoto.contains(soundName)
        print("pressed (soundName)")
        label.text = soundName
        playSoundFile(soundName)








        share|improve this answer













        Slightly modified (using array):



        var appearingInPhoto = ["omar", "john", "thomas"]

        @IBAction func buttonPressed(_ sender: UIButton)

        guard let soundName = sender.currentTitle else
        return

        if appearingInPhoto.contains(soundName)
        print("pressed (soundName)")
        label.text = soundName
        playSoundFile(soundName)









        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Feb 18 at 21:18









        Tomasz Czyżak

        1414




        1414






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184994%2femit-different-text-and-sound-when-each-of-three-buttons-is-pressed%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Chat program with C++ and SFML

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            Will my employers contract hold up in court?