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:
Timeris part of the Foundation framework. If you don’t haveimport Foundation(orimport 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
updateTimerfunction 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 aTimerparameter. 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 withCmd+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 astruct, 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 fromNSObject(e.g.,class MyClass: NSObjectinstead ofclass MyClass). For view controllers or UIKit classes, this is already handled since they inherit fromUIViewController→UIResponder→NSObject. - Check for Swift-only types in your function:
@objconly works with types Objective-C can recognize. If your function uses Swift-exclusive types likeResult, custom enums without@objcmodifiers, or certain generic types, adding@objcwill throw an error. Replace those types with Objective-C-compatible alternatives (likeNSErrorinstead of a custom Swift error enum if needed). - Avoid
@objcin extension-only classes: If you’re adding the function to an extension of a class that doesn’t inherit fromNSObject,@objcwon’t work. Ensure the base class isNSObject-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
相关产品推荐
相关产品推荐

