Swift 4.2 in Xcode 9.4.1 error with complicate code line

Below method can build success:

let maxValue = Float(max(UIScreen.main.bounds.size.width, UIScreen.main.bounds.size.height))
let dicQuality = profileConfig?.getImageQuality() ?? String: Any
let quality: Float = 0.5
let keyMap = dicQuality.keys.sorted { $0.toFloat(defaultValue: 0) > $1.toFloat(defaultValue: 0) }.first(where: { maxValue <= $0.toFloat(defaultValue: 0) })
if let key = keyMap {
return dicQuality.float(forKey: key, defaultValue: 0.5)
}
return quality

But Xcode yield error when I change to:


let maxValue = Float(max(UIScreen.main.bounds.size.width, UIScreen.main.bounds.size.height))
let dicQuality = profileConfig?.getImageQuality() ?? String: Any
let quality: Float = 0.5
if let keyMap = dicQuality.keys.sorted { $0.toFloat(defaultValue: 0) > $1.toFloat(defaultValue: 0) }.first(where: { maxValue <= $0.toFloat(defaultValue: 0) }) {
return dicQuality.float(forKey: keyMap, defaultValue: 0.5)
}
return quality

The error is:

My guess is the open bracket after "sorted" is being parsed as the beginning of the body of the if statement. If you surround the right-hand-side of let keyMap = in parens it should compile.

1 Like

George is correct. Please file a bug for the error message to be improved!

The bug here SR-8736. Please let me know if need any update.