我在Xcode中配置了一个所有异常断点:

有时候Xcode会停在一行上,比如:

[managedObjectContext save:&error];

具有以下回溯:

但是,如果您单击"继续",程序会继续运行,就好像什么都没有发生一样.

How can I ignore these "normal" exceptions, but still have the debugger stop on exceptions in my own code?

(我知道发生这种情况是因为Core Data内部抛出和捕获异常,而Xcode只是响应我的请求,每当抛出异常时暂停程序.但是,我想忽略这些,这样我就可以继续调试我自己的代码了!)

Moderators: this is similar to 100, but I think that question takes too long to get around to the point and doesn't have any useful answers. Can they be linked?

推荐答案

我编写了一个lldb脚本,让您可以用更简单的语法 Select 性地忽略Objective-C异常,它可以处理OS X、iOS模拟器以及32位和64位ARM.

Installation

  1. 将此脚本放入~/Library/lldb/ignore_specified_objc_exceptions.py或其他有用的位置.
import lldb
import re
import shlex

# This script allows Xcode to selectively ignore Obj-C exceptions
# based on any selector on the NSException instance

def getRegister(target):
    if target.triple.startswith('x86_64'):
        return "rdi"
    elif target.triple.startswith('i386'):
        return "eax"
    elif target.triple.startswith('arm64'):
        return "x0"
    else:
        return "r0"

def callMethodOnException(frame, register, method):
    return frame.EvaluateExpression("(NSString *)[(NSException *)${0} {1}]".format(register, method)).GetObjectDescription()

def filterException(debugger, user_input, result, unused):
    target = debugger.GetSelectedTarget()
    frame = target.GetProcess().GetSelectedThread().GetFrameAtIndex(0)

    if frame.symbol.name != 'objc_exception_throw':
        # We can't handle anything except objc_exception_throw
        return None

    filters = shlex.split(user_input)

    register = getRegister(target)


    for filter in filters:
        method, regexp_str = filter.split(":", 1)
        value = callMethodOnException(frame, register, method)

        if value is None:
            output = "Unable to grab exception from register {0} with method {1}; skipping...".format(register, method)
            result.PutCString(output)
            result.flush()
            continue

        regexp = re.compile(regexp_str)

        if regexp.match(value):
            output = "Skipping exception because exception's {0} ({1}) matches {2}".format(method, value, regexp_str)
            result.PutCString(output)
            result.flush()

            # If we tell the debugger to continue before this script finishes,
            # Xcode gets into a weird state where it won't refuse to quit LLDB,
            # so we set async so the script terminates and hands control back to Xcode
            debugger.SetAsync(True)
            debugger.HandleCommand("continue")
            return None

    return None

def __lldb_init_module(debugger, unused):
    debugger.HandleCommand('command script add --function ignore_specified_objc_exceptions.filterException ignore_specified_objc_exceptions')
  1. 将以下内容添加到~/.lldbinit:

    command script import ~/Library/lldb/ignore_specified_objc_exceptions.py

    如果您将~/Library/lldb/ignore_specified_objc_exceptions.py保存在其他位置,请将其替换为正确的路径.

Usage

  • 在Xcode中,添加断点以捕获所有Objective-C个异常
  • 编辑断点并使用以下命令添加调试器命令:
  • 这将忽略NSException -name匹配NSAccessibilityException-className匹配NSSomeException的异常

应该是这样的:

屏幕截图显示按照说明在Xcode中设置的断点

在您的情况下,您将使用ignore_specified_objc_exceptions className:_NSCoreData

有关脚本和更多详细信息,请参见http://chen.do/blog/2013/09/30/selectively-ignoring-objective-c-exceptions-in-xcode/.

Ios相关问答推荐

StackView UIKit间距似乎非常小的编程式编程

构建iOS项目失败.我们运行了";xcodeBuild";命令,但它退出并返回错误代码65-expo reaction ative

如何删除NavigationView中的默认透明标头?

快速 keys 串更新只有在第二次try 时才起作用

如何在集合视图中创建装饰视图?

更改订单时如何保存商品的位置?

使用 SceneKit 从 CapturedRoom.walls 重新创建 RoomPlan

将值传递给导航链接视图中的文本框

SwiftUI DatePicker 格式在每个设备上都不一样,如何让它总是显示相同的 Select 版本?

满足条件时 SwiftUI 动画背景 colored颜色 变化

带有属性包装器的不可用属性

DateFormatter 使用 TimeZone 和 Locale 的特定组合返回 nil

将 SwiftUI 与 @AppStorage 和动态函数调用一起使用

在 Swift 中按下返回键时在文本字段之间切换

Watchkit AppIcon - 名为AppIcon的应用图标集没有任何适用的内容

标题为 UIImage 的导航栏

使用 Xcode 4 的 iPhone 临时构建

如何找到 UILabel 的实际行数?

iOS应用程序每隔一次启动就会崩溃,找不到错误

Info.plist 实用程序错误:无法打开 Info.plist,因为没有这样的文件