从swift 3迁移到swift 4 – 无法将String转换为期望的String.Element

我将我的代码swift 3转换为 swift 4并在以下代码获取错误.即使我尝试使用flatmap来压平数组,我也会得到这个

Cannot convert value of type ‘String’ to expected argument type
‘String.Element’ (aka ‘Character’)

if favoritedProducts.contains("helloWorld") {}

下面的代码行不返回[String]而是返回'[String.Element]’如何将其转换为[String].如果我尝试将其转换为[String],它会说它总会失败.

let productIDs = allItems.flatMap{$0.productID}
如果您的Item类型具有类似String的非可选productID属性,请执行此操作
struct Item {
    let productID: String
}

你有一个项目数组

let allItems: [Item] = ...

然后,您可以使用map方法获取productID(s)数组

let productIDs = allItems.map { $0.productID }

现在productIDs是[String].

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...