检查数组中的一个元素是否包含在字符串中

Check if one element in array is contained in string

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

我需要循环数组并在字符串中搜索 .contains 的代码

我试过这个,但没找到

所以基本上我需要的功能是-> Bool有或没有。

你可以试试

let found = preferredCountryCurrenciesSymbols.filter { currency.contains([=10=])}.count != 0 // can use isEmpty also
print(found)

您链接的问题与您需要的相反。

您可以查找数组中的任何值是否包含在 currency:

let preferredCountryCurrenciesSymbols = ["USD", "EUR", "GBP", "HKD", "JPY", "RUB", "AUD", "CAD"]
let currency = "USDT"
if preferredCountryCurrenciesSymbols.first(where: { currency.contains([=10=]) }) != nil {
    print("found")
} else {
    print("not found")
}
var res = preferredCountryCurrenciesSymbols.contains { element in
                return element == currency
          }

您只需使用 contains(where:) 即可。

if preferredCountryCurrenciesSymbols.contains(where: { currency.contains([=10=]) }) {
    print("currency found...")
} else {
    print("currency not found...")
}