I am converting my code from swift 3 to swift 4 and getting this error in the following code. I am getting this even when I try to use flatmap to flatten an array
Cannot convert value of type 'String' to expected argument type 'String.Element' (aka 'Character')
if favoritedProducts.contains("helloWorld") {}
The below line of code does not return a [String] instead it is a '[String.Element]' How do I convert it to a [String]. If I try to cast it as a [String], it says it will always fail.
let productIDs = allItems.flatMap{$0.productID}
If you have an Item
type with a non optional productID
property of type String
like this
struct Item {
let productID: String
}
And you have an array of Item
let allItems: [Item] = ...
Then you can get an array of productID(s)
using the map
method
let productIDs = allItems.map { $0.productID }
Now productIDs
is [String]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With