I'm working on a project which is build on Swift3, then upgraded to Swift 4. By switching Toolchain > Swift Development Snapshot
to XCode 9.2
, XCode shows 3 errors related to compactMap
:
ERROR: Value of type '[ReceiptInfo]' (aka 'Array>') has no member 'compactMap'
let receiptItems = nonCancelledReceiptsInfo.compactMap { ReceiptItem(receiptInfo: $0) }
return receiptItems.compactMap {
if let expirationDate = $0.subscriptionExpirationDate {
return (expirationDate, $0)
}
return nil
}
Unfortunately I don't know so much about XCode and Swift. I need the equivalent Swift 4 code of it. I appreciate any kind help.
As mentioned by Hamish, it's correct. compactMap(:) was introduced in Swift 4.1 check here in Swift 4.0.x its flatMap(:) check here but it's now Deprecated.
let receiptItems = nonCancelledReceiptsInfo.flatMap { ReceiptItem(receiptInfo: $0) }
return receiptItems.flatMap {
if let expirationDate = $0.subscriptionExpirationDate {
return (expirationDate, $0)
}
return nil
}
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