2017-01-02 79 views
-1

轉換我的語法斯威夫特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!) 
    } 
} 
+1

請它所出現添加完整的錯誤消息和行 – mlidal

回答

0

參數change的值是Any得到了錯誤,編譯器不能推斷它應該是Int。您必須相應地投射類型。

最可靠的解決方案是可選的綁定所有的值和使用的NSKeyValueChangeKey相應的屬性:

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