预期的表达式错误 Xcode

Expected Expression Error Xcode

我 运行 在构建我的项目时遇到了一个错误,我似乎无法在任何地方找到修复。错误出现在这一行:;:CGFloat = info[UIKeyboardAnimationDurationUserInfoKey] as! CGFloat。这是我的代码:

func keyBoardWillShow(notification: NSNotification) {
    let info:NSDictionary = notification.userInfo!
    let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()

    let keyboardHeight:CGFloat =  keyboardSize.height - 40

    // Error is on this line
    _;:CGFloat = info[UIKeyboardAnimationDurationUserInfoKey] as! CGFloat

    let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardHeight, 0.0);
    self.tableView.contentInset = contentInsets
    self.tableView.scrollIndicatorInsets = contentInsets
}

因为代码行:

_;:CGFloat = info[UIKeyboardAnimationDurationUserInfoKey] as! CGFloat

不是有效的代码行。

您遇到此问题的行存在语法问题。注意:

_;:CGFloat = info[UIKeyboardAnimationDurationUserInfoKey] as! CGFloat

首先,您实际上有两个语句,因为分号会换行。所以:

_;
:CGFloat = info[UIKeyboardAnimationDurationUserInfoKey] as! CGFloat

如果你像这样打破行,你现在会得到两个错误。第一个说 '_' can only appear in a pattern or on the left side of an assignment,这是有道理的,因为下划线本身没有任何作用。你应该只删除这部分。

第二行缺少声明,因此编译器无法将该行解析为它理解的任何内容,因此 Expected expression。您希望将该表达式的结果存储在某处,并且您需要为此声明。你可以这样做:

let someName:CGFloat = info[UIKeyboardAnimationDurationUserInfoKey] as! CGFloat

调整 someName 以满足您的需要。我假设你需要那个表达式的结果来做某事,即使我没有看到它在赋值后被使用。如果你不需要它,只需考虑删除整个表达式。