0
let preferredCountryCurrenciesSymbols = ["USD", "EUR", "GBP", "HKD", "JPY", "RUB", "AUD", "CAD"]
let currency = "USDT"

I need code which loop the array and search in the string for .contains

I've tried with this, but it's not finding it

Check if array contains part of a string in Swift?

So basically the function I need is -> Bool have it or don't have it.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Bogdan Bogdanov
  • 882
  • 11
  • 36
  • 79
  • What result do you want? Do you want the index in `preferredCountryCurrenciesSymbols` for the value found inside `currency`? Do you just want to know if any value in the array is found inside `currency`? Show what you actually tried and clearly explain what result you got versus what you want. – rmaddy Jul 14 '19 at 22:40
  • If any value in the array is found inside currency – Bogdan Bogdanov Jul 14 '19 at 22:43

4 Answers4

0

You can try

let found = preferredCountryCurrenciesSymbols.filter { currency.contains($0)}.count != 0 // can use isEmpty also
print(found)
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
0

The question you linked is the opposite of what you need.

You can find if any value in the array is contained within currency:

let preferredCountryCurrenciesSymbols = ["USD", "EUR", "GBP", "HKD", "JPY", "RUB", "AUD", "CAD"]
let currency = "USDT"
if preferredCountryCurrenciesSymbols.first(where: { currency.contains($0) }) != nil {
    print("found")
} else {
    print("not found")
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 1
    You could use `contains(where:)` instead of `first(where:) != nil`. P.S. the downvote wasn't mine and not sure why anyone used one. – Dávid Pásztor Jul 14 '19 at 23:00
0
var res = preferredCountryCurrenciesSymbols.contains { element in
                return element == currency
          }
Yongjoon
  • 104
  • 1
  • 4
  • While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. It will be helpful for future users as well. – Sangam Belose Jul 15 '19 at 05:57
0

You can simply use contains(where:) to get that working.

if preferredCountryCurrenciesSymbols.contains(where: { currency.contains($0) }) {
    print("currency found...")
} else {
    print("currency not found...")
}
PGDev
  • 23,751
  • 6
  • 34
  • 88