Swift 4.2 in Xcode 9.4.1 error with complicate code line

xcode

(Lee) #1

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:


(George) #2

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.


(Jordan Rose) #3

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


(Lee) #4

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