How to compare [String]? and String in Swift? [closed]









up vote
-4
down vote

favorite












When I try to compare [String]? and String I get the error:




Binary operator '!=' cannot be applied to operands of type '[String]?' and 'String'




Could anyone tell me how to compare them?










share|improve this question















closed as unclear what you're asking by ColGraff, user6655984, TDG, greg-449, Rob Nov 10 at 12:42


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 2




    You'll have to be more clear on how you want to compare a collection of String to a single String. Do you want to know if any item in the collection is not equal to the single item?
    – ColGraff
    Nov 10 at 4:42










  • Just give us an example of what you are trying to do and show the code that gave you this error.
    – Rakesha Shastri
    Nov 10 at 4:44










  • Thanks for all you guys effort. Im new in Swift and just got confuse with the "Question Mark". And not realised the one Im comparing actually is an array. Finally understand just need to do like array![0]=="anystring"
    – Payne Chu
    Nov 11 at 1:53















up vote
-4
down vote

favorite












When I try to compare [String]? and String I get the error:




Binary operator '!=' cannot be applied to operands of type '[String]?' and 'String'




Could anyone tell me how to compare them?










share|improve this question















closed as unclear what you're asking by ColGraff, user6655984, TDG, greg-449, Rob Nov 10 at 12:42


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 2




    You'll have to be more clear on how you want to compare a collection of String to a single String. Do you want to know if any item in the collection is not equal to the single item?
    – ColGraff
    Nov 10 at 4:42










  • Just give us an example of what you are trying to do and show the code that gave you this error.
    – Rakesha Shastri
    Nov 10 at 4:44










  • Thanks for all you guys effort. Im new in Swift and just got confuse with the "Question Mark". And not realised the one Im comparing actually is an array. Finally understand just need to do like array![0]=="anystring"
    – Payne Chu
    Nov 11 at 1:53













up vote
-4
down vote

favorite









up vote
-4
down vote

favorite











When I try to compare [String]? and String I get the error:




Binary operator '!=' cannot be applied to operands of type '[String]?' and 'String'




Could anyone tell me how to compare them?










share|improve this question















When I try to compare [String]? and String I get the error:




Binary operator '!=' cannot be applied to operands of type '[String]?' and 'String'




Could anyone tell me how to compare them?







swift string compare






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 5:12









rmaddy

236k27306373




236k27306373










asked Nov 10 at 4:30









Payne Chu

146313




146313




closed as unclear what you're asking by ColGraff, user6655984, TDG, greg-449, Rob Nov 10 at 12:42


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as unclear what you're asking by ColGraff, user6655984, TDG, greg-449, Rob Nov 10 at 12:42


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









  • 2




    You'll have to be more clear on how you want to compare a collection of String to a single String. Do you want to know if any item in the collection is not equal to the single item?
    – ColGraff
    Nov 10 at 4:42










  • Just give us an example of what you are trying to do and show the code that gave you this error.
    – Rakesha Shastri
    Nov 10 at 4:44










  • Thanks for all you guys effort. Im new in Swift and just got confuse with the "Question Mark". And not realised the one Im comparing actually is an array. Finally understand just need to do like array![0]=="anystring"
    – Payne Chu
    Nov 11 at 1:53













  • 2




    You'll have to be more clear on how you want to compare a collection of String to a single String. Do you want to know if any item in the collection is not equal to the single item?
    – ColGraff
    Nov 10 at 4:42










  • Just give us an example of what you are trying to do and show the code that gave you this error.
    – Rakesha Shastri
    Nov 10 at 4:44










  • Thanks for all you guys effort. Im new in Swift and just got confuse with the "Question Mark". And not realised the one Im comparing actually is an array. Finally understand just need to do like array![0]=="anystring"
    – Payne Chu
    Nov 11 at 1:53








2




2




You'll have to be more clear on how you want to compare a collection of String to a single String. Do you want to know if any item in the collection is not equal to the single item?
– ColGraff
Nov 10 at 4:42




You'll have to be more clear on how you want to compare a collection of String to a single String. Do you want to know if any item in the collection is not equal to the single item?
– ColGraff
Nov 10 at 4:42












Just give us an example of what you are trying to do and show the code that gave you this error.
– Rakesha Shastri
Nov 10 at 4:44




Just give us an example of what you are trying to do and show the code that gave you this error.
– Rakesha Shastri
Nov 10 at 4:44












Thanks for all you guys effort. Im new in Swift and just got confuse with the "Question Mark". And not realised the one Im comparing actually is an array. Finally understand just need to do like array![0]=="anystring"
– Payne Chu
Nov 11 at 1:53





Thanks for all you guys effort. Im new in Swift and just got confuse with the "Question Mark". And not realised the one Im comparing actually is an array. Finally understand just need to do like array![0]=="anystring"
– Payne Chu
Nov 11 at 1:53













2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










You can't compare a string with an optional array of strings, they have two different types: The equality operator ==, as defined in the standard library, can't compare a string to an optional array of strings.



If you want to check if an optional array contains a string, then use the following:



let array: [String]? = ["hello", "world", "✋🏻"]
let result = array?.contains("hello")


result would be an optional boolean that you may unwrap later.






share|improve this answer



























    up vote
    0
    down vote













    var array = ["First", "Second"]
    let loneString = "Second"



    for k in 0..<array.count 

    if array[k] != loneString

    print("they are not same")








    share|improve this answer


















    • 2




      This doesn't even compile (you can't unwrap a string that isn't optional) + array is optional in the question
      – Carpsen90
      Nov 10 at 11:04


















    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    You can't compare a string with an optional array of strings, they have two different types: The equality operator ==, as defined in the standard library, can't compare a string to an optional array of strings.



    If you want to check if an optional array contains a string, then use the following:



    let array: [String]? = ["hello", "world", "✋🏻"]
    let result = array?.contains("hello")


    result would be an optional boolean that you may unwrap later.






    share|improve this answer
























      up vote
      1
      down vote



      accepted










      You can't compare a string with an optional array of strings, they have two different types: The equality operator ==, as defined in the standard library, can't compare a string to an optional array of strings.



      If you want to check if an optional array contains a string, then use the following:



      let array: [String]? = ["hello", "world", "✋🏻"]
      let result = array?.contains("hello")


      result would be an optional boolean that you may unwrap later.






      share|improve this answer






















        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        You can't compare a string with an optional array of strings, they have two different types: The equality operator ==, as defined in the standard library, can't compare a string to an optional array of strings.



        If you want to check if an optional array contains a string, then use the following:



        let array: [String]? = ["hello", "world", "✋🏻"]
        let result = array?.contains("hello")


        result would be an optional boolean that you may unwrap later.






        share|improve this answer












        You can't compare a string with an optional array of strings, they have two different types: The equality operator ==, as defined in the standard library, can't compare a string to an optional array of strings.



        If you want to check if an optional array contains a string, then use the following:



        let array: [String]? = ["hello", "world", "✋🏻"]
        let result = array?.contains("hello")


        result would be an optional boolean that you may unwrap later.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 11:26









        Carpsen90

        6,59062557




        6,59062557






















            up vote
            0
            down vote













            var array = ["First", "Second"]
            let loneString = "Second"



            for k in 0..<array.count 

            if array[k] != loneString

            print("they are not same")








            share|improve this answer


















            • 2




              This doesn't even compile (you can't unwrap a string that isn't optional) + array is optional in the question
              – Carpsen90
              Nov 10 at 11:04















            up vote
            0
            down vote













            var array = ["First", "Second"]
            let loneString = "Second"



            for k in 0..<array.count 

            if array[k] != loneString

            print("they are not same")








            share|improve this answer


















            • 2




              This doesn't even compile (you can't unwrap a string that isn't optional) + array is optional in the question
              – Carpsen90
              Nov 10 at 11:04













            up vote
            0
            down vote










            up vote
            0
            down vote









            var array = ["First", "Second"]
            let loneString = "Second"



            for k in 0..<array.count 

            if array[k] != loneString

            print("they are not same")








            share|improve this answer














            var array = ["First", "Second"]
            let loneString = "Second"



            for k in 0..<array.count 

            if array[k] != loneString

            print("they are not same")









            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 11 at 7:42









            mrvincenzo

            3,39832142




            3,39832142










            answered Nov 10 at 8:29









            Hope

            5493828




            5493828







            • 2




              This doesn't even compile (you can't unwrap a string that isn't optional) + array is optional in the question
              – Carpsen90
              Nov 10 at 11:04













            • 2




              This doesn't even compile (you can't unwrap a string that isn't optional) + array is optional in the question
              – Carpsen90
              Nov 10 at 11:04








            2




            2




            This doesn't even compile (you can't unwrap a string that isn't optional) + array is optional in the question
            – Carpsen90
            Nov 10 at 11:04





            This doesn't even compile (you can't unwrap a string that isn't optional) + array is optional in the question
            – Carpsen90
            Nov 10 at 11:04




            Popular posts from this blog

            Kleinkühnau

            Makov (Slowakei)

            Deutsches Schauspielhaus