This switch statement is switching over the cases of an Enum. The leading dot in .Success
tells you that Success
is one of the cases of whatever Enum this is.
Swift Enums allow you to use associated values (not to be confused with associated values of Protocols) which let the cases of an Enum store data. The example the Swift docs give is of a Barcode Enum that encodes a standard bar code and a QR code:
enum Barcode {
case UPCA(Int, Int, Int, Int)
case QRCode(String)
}
This says that UPCA bar codes are given four Int values and QR codes are given a single String value, which can be read to and written from. For instance, var productBarcode = Barcode.UPCA(8, 85909, 51226, 3)
creates an object of type Barcode
with value .UPCA(8, 85909, 51226, 3)
; you don't need the full name of the Enum (just the leading dot) because the type can be inferred by the compiler.
When switching over the cases of an Enum, you may assign the associated type values of the matching case to variables and then use those variables in the block for that case.
switch productBarcode {
case let .UPCA(numberSystem, manufacturer, product, check):
print("UPC-A: \(numberSystem), \(manufacturer), \(product), \(check).")
case let .QRCode(productCode):
print("QR code: \(productCode).")
}
If productBarcode
is case .UPCA
, then it matches the first case and those variables are assigned accordingly, and similarly if it is case .QRCode
.
An underscore in the variable assignment list simply tells Swift to discard the value there instead of assigning it to a variable. In the case of your code, it is only assigning the first of the three associated type values to a constant named upload
.