2016-01-22 55 views
0

我读了关于这个错误的信息,我知道它在编译器不知道它需要返回什么类型时发出警报,但是这个错误并没有出现,并且我不知道它为什么出现在今天。模糊使用下标错误

这是我的代码:

func animateCounter(from: Int, to: Int) { 
    timer = NSTimer.scheduledTimerWithTimeInterval(duration, target: self, selector: "increaseCounter", userInfo: ["from": from, "to": to], repeats: false) 
} 

func increaseCounter() { 
    let from = timer.userInfo!["from"] as! Int 
    let to = timer.userInfo!["to"] as! Int 
} 

我把我的fromto变量均为整数,那么,为什么我得到这个错误?

+1

你必须能够之前投了'userInfo'到词典通过下标访问其内容。 – luk2302

+0

这很有趣,但它现在起作用。我没有做任何改变= / –

回答

0

演员USERINFO到Optional([String:AnyObject]),然后再投userInfo["from"], userInfo["to"]Optional(Int)

你将需要自己解开结果:)

func increaseCounter() { 
    let userInfo = timer.userInfo as? [String: AnyObject] 
    if let dict = userInfo { 
     let from = dict["from"] as? Int 
     let to = dict["to"] as? Int 
    } 
}