2016-01-22 57 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 
    } 
}