预期的表达式错误Xcode

时间:2016-01-03 05:02:06

标签: ios swift expression

在构建项目时遇到一个错误,我似乎无法在任何地方找到修复程序。错误显示在此行:;: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
}

2 个答案:

答案 0 :(得分:2)

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

_;: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以满足您的需求。我假设您需要该表达式的结果,即使我在分配后没有看到它被使用。如果您不需要它,请考虑删除整个表达式。

答案 1 :(得分:-2)

这是因为代码行:

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

不是有效的代码行。

相关问题