我正在开发一个基于
Swift3的项目,然后升级到
Swift 4.通过切换工具链> Swift Development Snapshot to XCode 9.2,XCode显示了与compactMap相关的3个错误:
错误:类型'[ReceiptInfo]'(又名’Array>’)的值没有成员’compactMap’
let receiptItems = nonCancelledReceiptsInfo.compactMap { ReceiptItem(receiptInfo: $0) } return receiptItems.compactMap { if let expirationDate = $0.subscriptionExpirationDate { return (expirationDate,$0) } return nil }
不幸的是,我对XCode和Swift了解不多.我需要等效的Swift 4代码.我感谢任何帮助.
解决方法
正如Hamish所说,这是正确的. compactMap(:)是在Swift 4.1中引入的,检查
here在Swift 4.0.x中的flatMap(:)检查
here,但它现在已经过时了.
let receiptItems = nonCancelledReceiptsInfo.flatMap { ReceiptItem(receiptInfo: $0) } return receiptItems.flatMap { if let expirationDate = $0.subscriptionExpirationDate { return (expirationDate,$0) } return nil }