You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift代码中Type 'Timer' has no member 'scheduledTimer'错误的解决求助

Swift代码中Type 'Timer' has no member 'scheduledTimer'错误的解决求助

各位大佬好,我最近写Swift代码时碰到了个棘手的问题:明明已经导入了UIKit和Foundation框架,可调用Timer的scheduledTimer方法时,一直报错“Type 'Timer' has no member 'scheduledTimer'”。我一开始以为是导入语句的问题,反复检查了好几次,可这个错误就是消不掉,实在没辙了来求助大家!

我的代码片段如下:

import UIKit 
import Foundation  

class TimerInterfaceViewController: UIViewController {  
    override func viewDidLoad() {  
        super.viewDidLoad()                                      
        // 想每隔2秒重复触发一次定时器
        let timer = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: true) { timer in
            // 后续要添加定时器触发的逻辑
        }
    }
}

有没有人能帮我看看这到底是哪里出问题了,怎么才能把这个错误去掉呀?

备注:内容来源于stack exchange,提问作者Train Lover

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 17:13:10