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

Swift中Timer问题求解:代码与讲师一致却无法运行

Troubleshooting Your Timer & Syntax Highlighting Issues

Hey there! Let’s walk through fixing these problems you’re facing with your app development code—syntax highlighting not working, and errors when adding @objc to your function.

1. Why updateTimer and Timer.scheduled aren’t getting syntax highlighting

Xcode’s syntax highlighting relies on proper framework imports and correct syntax recognition. Here’s what to check:

  • Make sure you’ve imported Foundation: Timer is part of the Foundation framework. If you don’t have import Foundation (or import UIKit, which includes Foundation) at the top of your file, Xcode won’t recognize these APIs, so no highlighting. Add this line first thing in your Swift file.
  • Verify your updateTimer function signature: If this function is meant to be called by a Timer via a selector, it needs a signature Objective-C can understand. For example, it should look like @objc func updateTimer(_ timer: Timer)—the underscore for the external parameter name, and a Timer parameter. If your signature is missing this, Xcode might not flag it as a valid selector method, leading to no highlighting.
  • Reset Xcode’s index: Sometimes Xcode’s code indexing gets glitched. Try cleaning your build folder with Cmd+Shift+K, then clear the index with Cmd+Option+Shift+K, restart Xcode, and re-open your project. This often fixes random syntax highlighting issues.

2. Fixing the @objc function error

The @objc attribute lets Swift code interact with Objective-C, but it has strict requirements:

  • Your function must be in a class that inherits from NSObject: If you’re working with a struct, you can’t use @objc—structs don’t support the Objective-C runtime. If your class is a custom one without a parent, change it to inherit from NSObject (e.g., class MyClass: NSObject instead of class MyClass). For view controllers or UIKit classes, this is already handled since they inherit from UIViewController → UIResponder → NSObject.
  • Check for Swift-only types in your function: @objc only works with types Objective-C can recognize. If your function uses Swift-exclusive types like Result, custom enums without @objc modifiers, or certain generic types, adding @objc will throw an error. Replace those types with Objective-C-compatible alternatives (like NSError instead of a custom Swift error enum if needed).
  • Avoid @objc in extension-only classes: If you’re adding the function to an extension of a class that doesn’t inherit from NSObject, @objc won’t work. Ensure the base class is NSObject-compliant.

Example of a working Timer setup

Here’s a snippet that should have proper syntax highlighting and no @objc errors:

import UIKit // Includes Foundation automatically

class TimerViewController: UIViewController {
    var timer: Timer?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Timer.scheduled should highlight here
        timer = Timer.scheduledTimer(
            timeInterval: 1.0,
            target: self,
            selector: #selector(updateTimer(_:)),
            userInfo: nil,
            repeats: true
        )
    }
    
    // updateTimer should highlight green here
    @objc func updateTimer(_ timer: Timer) {
        print("Timer ticked!")
        // Add your timer logic here
    }
    
    deinit {
        timer?.invalidate() // Clean up the timer when the view controller is destroyed
    }
}

If you still run into issues, share a bit more of your full code (like the class definition and the full updateTimer function) and we can dig deeper!

内容的提问来源于stack exchange,提问作者fabiocattolico

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 11:07:36