简体   繁体   English

下标错误使用

[英]Ambiguous use of subscript error

I read about this error and I know that it alerts when the compiler does not know what type it needs to return, but this error did not appear earlier and I do not know why it appears today. 我读到此错误,我知道它会在编译器不知道需要返回哪种类型时发出警报,但是此错误并未较早出现,并且不知道为什么今天会出现。

This is my code: 这是我的代码:

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
}

I set that my from and to variables are Integers, so why I get this error? 我将fromto变量设置为Integers,那么为什么会出现此错误?

Cast userInfo to Optional([String:AnyObject]) first and then cast userInfo["from"], userInfo["to"] to Optional(Int) 首先将userInfo转换为Optional([String:AnyObject]) ,然后将userInfo["from"], userInfo["to"]Optional(Int)

Your will need to unwrap the result by yourself :) 您将需要自己解开结果:)

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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM