我無法弄清楚什麼是錯的以下內容:這個Swift語法有什麼問題?
fileprivate func showInlineErrorMessage(_ message: String, forField textField: UITextField) {
// Show error message
errorLabel.text = message
errorLabel.isHidden = false
// Highlight field on which the error is
[usernameTextField, passwordTextField, birthYearTextField, genderTextField].forEach {
updateHighlightOnTextField($0!, highlight: $0 === textField)
}
}
的特定部分是$0 === textField
編譯器顯示以下錯誤:
"?" must be followed by a call, member lookup, or a subscript
這是舊代碼,所以最近似乎有些事情可能發生了變化。
被調用的功能
fileprivate func updateHighlightOnTextField(_ textField: UITextField, highlight: Bool) {
let highlightView = textField.superview!
highlightView.layer.borderWidth = highlight ? 1.0 : 0.0
highlightView.layer.borderColor = highlight ? DefaultTheme.lineColorError.cgColor : nil
}
'updateHighlightOnTextField'方法的簽名是什麼?爲什麼你強制展開'$ 0'作爲第一個參數?是否有一個或多個文本字段是可選的?如果第一個參數是force-unwrapped,爲什麼不是第二個? – rmaddy
我剛剛更新了上面的問題與其他信息。 –