Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migration from swift 3 to swift 4 - Cannot convert String to expected String.Element

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}
like image 659
Nevin Avatar asked Sep 23 '17 20:09

Nevin


1 Answers

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].

like image 179
Luca Angeletti Avatar answered Nov 05 '22 16:11

Luca Angeletti