Xcode中print无控制台输出但NSLog正常,求问题成因
你创建Production scheme后,出现print语句在控制台无输出但NSLog正常的情况,且两个scheme都受影响,可以从以下几个方向排查:
检查编译优化等级
打开项目对应scheme的Build Settings,找到Optimization Level:- Debug/Development模式下应设置为
None [-O0],如果误设为Fastest, Smallest [-Os]或其他优化选项,编译器会移除print这类调试输出。即使是Development scheme,也可能因误操作修改了该设置。
- Debug/Development模式下应设置为
查看Swift编译自定义标志
在Build Settings的Other Swift Flags中,检查是否添加了-O等会触发代码优化的标志,或者是否全局定义了类似DEBUG=0的宏,导致print被条件编译屏蔽。确认控制台过滤设置
Xcode控制台顶部的过滤选项需设置为All Output,避免因误选Debug Output或输入了过滤关键词,导致print输出被隐藏。排查Firebase配置的潜在干扰
暂时注释掉FirebaseConfiguration.shared.setLoggerLevel(.min)这行代码,测试print是否恢复输出。虽然该设置仅针对Firebase日志,但不排除特殊场景下的意外冲突。检查scheme的环境变量
进入scheme的Run->Arguments->Environment Variables,查看是否设置了OS_ACTIVITY_MODE=disable。该变量会关闭大部分控制台输出,但NSLog不受影响,若存在则删除或改为enable。验证全局print重定义
搜索项目中的func print,确认是否存在全局重写的print函数(比如自定义了空实现),导致系统print未被调用。
示例代码:
#if Production guard let filePath = Bundle.main.path(forResource: "GoogleService-Info-PROD", ofType: "plist") else { return } if let options = FirebaseOptions.init(contentsOfFile: filePath) { FirebaseApp.configure(options: options) } print("[FIREBASE] Production mode.") NSLog("[FIREBASE] Production mode.") #elseif Development guard let filePath = Bundle.main.path(forResource: "GoogleService-Info-DEV", ofType: "plist") else { return } if let options = FirebaseOptions.init(contentsOfFile: filePath) { FirebaseApp.configure(options: options) } print("FIREBASE - Development mode.") NSLog("FIREBASE - Development mode.") #endif FirebaseConfiguration.shared.setLoggerLevel(.min) UNUserNotificationCenter.current().delegate = self let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] UNUserNotificationCenter.current().requestAuthorization(options: authOptions) { _, _ in } application.registerForRemoteNotifications() Messaging.messaging().delegate = self
内容的提问来源于stack exchange,提问作者martin wotterman

