如何在Swift中获取堆栈跟踪错误? [英] How can I get stack trace error in Swift?

查看:882
本文介绍了如何在Swift中获取堆栈跟踪错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Objective-C中,无论何时应用程序崩溃,我可以获取堆栈跟踪,以便在 AppDelegate 中查看导致错误的最后一个方法, (UIApplication *)应用程序didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSSetUncaughtExceptionHandler($)

In Objective-C, Whenever an application crashes, I can get stack trace to see where is the last method that causes the error by using this code in AppDelegate

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 {
     NSSetUncaughtExceptionHandler(&myExceptionHandler);
     return YES;
 }

void myExceptionHandler(NSException *exception)
{
    NSArray *stack = [exception callStackReturnAddresses];
    NSLog(@"Stack trace: %@", stack);

    NSLog(@"MyExceptionHandler");
}

它将打印堆栈跟踪日志到控制台,我可以使用它来调试问题的原因而不是结束于 main.m ,没有信息

and it will print the stack trace log to console which I can use to debug the cause of the problem instead of ending up at main.m with no information

那么我该怎么做在Swift?

So how can I do this in Swift?

推荐答案

如果我正确理解你,我想你正在寻找的是一个 异常断点 ,它的作用就像一个常规的断点,但是在抛出异常时调用。这样一来,它就会在应用程序正确抛出异常的情况下停止,所以您可以在崩溃时看到方法,代码行和变量值。

可以通过转到导航器中的断点导航器选项卡,点击左下角的加号并选择添加例外断点。

If I understand you correctly, I think what you are looking for is an exception breakpoint, which functions just like a regular breakpoint but is called whenever an exception is thrown. That way, it will stop your application right where the exception was thrown, so you can see the method, line of code, and variable values at the moment of the crash.

This can be set by going to the Breakpoint Navigator tab in the Navigator, clicking the plus at the bottom left and selecting "Add Exception Breakpoint ".


异常断点不能通过右键单击并选择编辑断点,通过各种选项进行编辑。


The Exception Breakpoint can than be edited with various options by right-clicking on it and selecting "Edit Breakpoint".

这篇关于如何在Swift中获取堆栈跟踪错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆