swift - Checking if an array contains a string -


i'm trying evaluate if entered string partially matches item in array. when use following method in playgrounds seems work properly. however, when use exact same method in xcode 9.0 beta 6 (9m214v) doesn't return correct answer.

func isvalid(_ item: string)  -> bool {     let whitelist = ["https://apple.com","https://facebook.com","https://stackoverflow.com"]      return whitelist.contains(where: {$0 <= item ? true : false  }) } 

there's anomalies when passed in "https://twitter.com" it'll return true. nuts? , while i'm here, have different approach solve problem?

theres anomalies when passed in "https://twitter.com" it'll return true.

whether version of swift 3 or 4, based on code snippet should same true result! why?

because logic of contains(where:) of doing comparison related logic of equality of given elements, i,e cannot use contains array of non-equatable elements. make more clear:

"https://apple.com" <= "https://twitter.com" "https://facebook.com" <= "https://twitter.com" "https://stackoverflow.com" <= "https://twitter.com" 

the result true all statements ('t' character greater 'a', 'f' , 's').

thus:

"https://zwebsite" <= "https://twitter.com" 

would returns false ('t' less 'z').

however, expected result, implement function this:

func isvalid(_ item: string)  -> bool {      let whitelist = ["https://apple.com","https://facebook.com","https://stackoverflow.com"]      return whitelist.contains { $0 == item } } 

or shorter contains:

return whitelist.contains(item) 

which leads let

isvalid("https://twitter.com") 

to returns false.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -