Swift中print函数无法输出到控制台的问题排查求助
print() Output in Xcode Hey there! It's super frustrating when print() doesn't show up in the console even though you know the code is running. Let's walk through the most common fixes for this issue:
Double-check console filtering settings
Even though you mentioned selecting "All Output", take a quick second to confirm:- The filter dropdown in the Xcode console is definitely set to All Output (not limited to a specific process or "Process" mode).
- The search bar above the console is empty—accidental keywords can easily filter out your output without you noticing.
Check for
OS_ACTIVITY_MODEin Scheme settings
This is a super common culprit! If you ever added this environment variable to suppress noisy system logs, it might be blocking your ownprint()statements too:- Go to
Product > Scheme > Edit Scheme(or hitCmd + <). - Select the
Runtab, then switch to theArgumentspane. - Look in the Environment Variables section for
OS_ACTIVITY_MODE. If it’s set todisable, delete the variable entirely or change its value toenable.
- Go to
Test with
NSLogas a fallback
Temporarily swap yourprint()call withNSLog("viewDidLoad")and re-run the app. IfNSLogshows up, you might have a customprint()implementation somewhere in your project that’s redirecting output away from the console.Reset device/simulator connection
- For physical devices: Unplug and re-plug your device, then make sure Xcode’s console is connected to the correct device (check the dropdown at the top of the Xcode window).
- For simulators: Quit the simulator completely, restart it, and re-run your project. Simulators can sometimes get stuck and stop logging output unexpectedly.
Clean build files and restart Xcode
Xcode’s cache can cause weird, unexplained glitches:- Press
Cmd + Shift + Kto clean the project. - Press
Cmd + Option + Shift + Kto delete the derived data folder. - Quit Xcode entirely, then reopen and run your project again.
- Press
Just to confirm, your code itself looks totally correct:
override func viewDidLoad() { super.viewDidLoad() print("viewDidLoad") }
Work through these steps one by one, and you should get your print() output back in no time!
内容的提问来源于stack exchange,提问作者Trevor

