If case let, guard case let, for case let

At the 16:43 mark of WWDC20 session 10189 is this code snippet:

func handleIncomingFile(_ incomingResourceURL: URL, with name: String, from fromID: String) {
    guard
         case let safeFileName = name.lastPathComponent, 
         safeFileName.count > 0,
         safeFileName != "..", safeFileName != "." else { return }

    let destinationFileURL = URL(fileURLWithPath: NSTemporaryDirectory())
                                 .appendingPathComponent(safeFileName)

    // Copy the file into a temporary directory
    try! FileManager.default.copyItem(at: incomingResourceURL, to: destinationFileURL)

}

There is no documentation of guard case let in the official language guide. All resources I found online are dated about 4 years back, written for Swift 2. The best one I found is this write-up that covers if case let, guard case let, and for case let.

Because there were a lot of source breaking changes between Swift 2 and 5, and because these expressions are not documented in the language guide, I wonder if they have been replaced by other expressions?

It’s still there within grammars. if and guard accept list of conditions and case condition is one of those.

1 Like

Thanks!

It’s worth noting that just because something is permitted by the grammar, that does not automatically guarantee it is valid in the language:

Terms of Service

Privacy Policy

Cookie Policy