Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix `Value of type '[ReceiptInfo]' (aka ...) has no member 'compactMap'`?

Tags:

swift

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.

like image 869
Kardo Avatar asked Feb 11 '18 12:02

Kardo


1 Answers

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
}
like image 105
Ashish Avatar answered Sep 28 '22 07:09

Ashish