加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何在Swift中恢复堆栈跟踪?

发布时间:2020-12-14 02:25:11 所属栏目:百科 来源:网络整理
导读:如何在 Swift中恢复堆栈跟踪,以便我可以找出在崩溃之前调用的最后一个方法? 我想在Swift中复制的是C#中的这个方法: public static Assembly GetComponentInInvocationDegree(int invocationDegree) { if (invocationDegree 0) throw new ArgumentOutOfRang
如何在 Swift中恢复堆栈跟踪,以便我可以找出在崩溃之前调用的最后一个方法?

我想在Swift中复制的是C#中的这个方法:

public static Assembly GetComponentInInvocationDegree(int invocationDegree)
    {
        if (invocationDegree < 0) throw new ArgumentOutOfRangeException("Cannot set out of range value: invocation degree must be greater than or equal to zero");

        var stackInvocacion = new StackTrace().GetFrames();
        if (invocationDegree > 0)
        {
            int invokingAssemblyIndex = 0;
            string currentDegreeComponentName;
            try
            {
                for (int currentDegree = 0; currentDegree < invocationDegree; currentDegree++)
                {
                    currentDegreeComponentName = stackInvocacion[invokingAssemblyIndex].GetMethod().ReflectedType.Assembly.GetName().Name;
                    while (stackInvocacion[invokingAssemblyIndex].GetMethod().ReflectedType.Assembly.GetName().Name == currentDegreeComponentName) invokingAssemblyIndex++;                        
                }
            }
            catch (Exception ex) { throw new InvalidOperationException(string.Format("Cannot get component in invocation degree: invocation degree {0} does not exist",invocationDegree),ex); }

            return stackInvocacion[invokingAssemblyIndex].GetMethod().ReflectedType.Assembly;
        }

        return stackInvocacion[0].GetMethod().ReflectedType.Assembly;
    }
在许多情况下,您可以使用try / catch机制.
do {
    try ... // risky business goes here
} catch let error as NSError {
    print("Error: (error.domain)")
    println(NSThread.callStackSymbols())
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读