Swift3 对成员'的模糊引用;下标';在Swift 3错误中

Swift3 对成员'的模糊引用;下标';在Swift 3错误中,swift3,xcode8,Swift3,Xcode8,将我的语法转换为Swift 3。当我这样做的时候,我得到了这个代码的错误 open override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { if keyPath! == "animatingTab" && change!["new"]

将我的语法转换为Swift 3。当我这样做的时候,我得到了这个代码的错误

open override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {

    if keyPath! == "animatingTab" && change!["new"]!.isEqual(NSNumber (value: 2 as Int))// here i got error{

        UIView.animate(withDuration: 0.25, delay: 0, options: UIViewAnimationOptions.curveEaseIn, animations: {
            self.settingsView.frame.origin = CGPoint(x: LayoutService.width(widthPercentage: 20), y: 0)
            self.settingsView.setShadows()
        }, completion: nil)

    }
    else if keyPath! == "animatingTab" && !change!["new"]!.isEqual(NSNumber(value: 2 as Int))// here i got error {

        UIView.animate(withDuration: 0.25, delay: 0, options: UIViewAnimationOptions.curveEaseOut, animations: {
            self.settingsView.frame.origin = CGPoint(x: LayoutService.width(widthPercentage: 100), y: 0)
            self.settingsView.clearShadows()
        }, completion: nil)

    }

    if keyPath! == "currentUser" {
        self.settingsView.profileView.buildProfileForUser(user: AccountService.sharedInstance.currentUser!)
    }
}

参数
change
的值是
Any
,编译器无法推断它应该是
Int
。您必须相应地转换类型

最可靠的解决方案是可选绑定所有值并使用
NSKeyValueChangeKey
的适当属性:

if let path = keyPath, path == "animatingTab"
   let new = change?[.newKey] as? Int, new == 2 {  ...

请添加完整的错误消息及其出现的行