ScrollView swift 编译器错误

ScrollView swift compiler error

我不断收到此错误:

Ambiguous use of 'size

我到处查看并尝试了一些技巧,但它不起作用,我也不知道为什么。

import UIKit

class ScrollView: UIScrollView {
    var textField: UITextField!

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!

        self.backgroundColor = UIColor(white: 1, alpha: 0)

    }

    func registerForKeyboardNotifications() {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.keyboardWasShown), name: UIKeyboardDidShowNotification, object: nil)

        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.keyboardWillBeHidden), name: UIKeyboardDidShowNotification, object: nil)
    }

    func keyboardWasShown(notification: NSNotification) {
        let info: NSDictionary = notification.userInfo!
        //HERE=>   let size = info.objectForKey(UIKeyboardFrameBeginUserInfoKey)?.size  /* THIS IS THE LINE OF THE ERROR */
        let insets = UIEdgeInsetsMake(0.0, 0.0, size!.height, 0.0)

        self.contentInset = insets
        self.scrollIndicatorInsets = insets

        var rect = self.frame
        rect.size.height -= size!.height

    }

    func keyboardWillBeHidden(notification: NSNotification) {
        let insets = UIEdgeInsetsZero
        self.contentInset = insets
        self.scrollIndicatorInsets = insets
    }

    func textFieldDidBeginEditing(textField: UITextField) {
        self.textField = textField
    }

    func textFieldDidEndEditing(textField: UITextField) {
        self.textField = nil
    }

    /**
     * Called when 'return' key pressed. return NO to ignore.
     */
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }

    /**
     * Called when the user click on the view (outside the UITextField).
     */
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.endEditing(true)
    }
}

这是我出错的地方。我使用 Swift 2.3 和 Xcode 8。 我试图更改我的功能,但出现了更多错误。

您必须转换 objectForKey 的 return。 objectForKey returns 类型 "Any" 并且您需要来自 CGRect class.

的方法
let size = (info.objectForKey(UIKeyboardFrameBeginUserInfoKey) as! CGRect)?.size