NSLog 上.mm 文件

问题描述 投票:0回答:4

如果文件扩展名为 .mm,如何将某些内容打印到调试器控制台?

我尝试了

NSLog
printf
,但调试器控制台上没有显示任何内容。

objective-c cocoa objective-c++
4个回答
3
投票

NSLog 在 .mm 文件以及 printf 中工作得很好。即使

std::cout << "foo";
在 .mm 文件中也能很好地工作,因为扩展名将文件标记为 Objective-C++ 文件,因此您可以在其中混合使用 C、C++ 和 ObjC。

如果没有打印出任何内容,请确保程序确实达到了您设置的点

NSLog
,您可以为此放置几个断点,然后单步执行程序。


0
投票

您的问题与编译单元的类型无关(“.mm”对于 Objective-C++ 或“.m”对于纯 Objective-C)。

NSLog
以及
printf
支持两种语言。

你的问题在别处。


0
投票

尝试导入

将此行添加到类的顶部,这可能会有所帮助。

#import <Foundation/Foundation.h>

0
投票

对于从未使用过的 XCode 版本,建议在 appdelegate 中使用 OS_LOG 而不是 NSLOG,因为您可以获得有用的类别过滤。不过声明有点奇怪。您在类外部创建一个 nil 对象,然后需要在每个函数的函数体中创建实际的记录器对象。当尝试在

os_log_create
行下方声明
@implementation AppDelegate
时,我会遇到很多错误。

#import "AppDelegate.h"
#import <os/log.h>

os_log_t logger = nil;

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  // other configuration code above
  // I chose to give it the category of "AppDelegate"
  logger = os_log_create("com.bundle.id", "AppDelegate"); 

  self.moduleName = @"main";
  // You can add your custom initial props in the dictionary below.
  // They will be passed down to the ViewController used by React Native.
  self.initialProps = @{};
  os_log(logger, "This is gonna log the message!");
  os_log(logger, "In the logs explorer, you can now Filter by category = AppDelegate");
  
  bool didFinish = [super application:application didFinishLaunchingWithOptions:launchOptions];
   
   return didFinish;
}

- (void)applicationWillTerminate:(UIApplication *)application {
  logger = os_log_create("com.bundle.id", "AppDelegate");
  os_log(logger, "App terminated! Good bye!");
}

@end
© www.soinside.com 2019 - 2024. All rights reserved.