How to set a conditional breakpoint in Xcode based on an object string property?

后端 未结 5 1122
鱼传尺愫
鱼传尺愫 2020-12-02 05:21

I\'m looking to be able to have the debugger break when it reaches a particular string match. As an example, I might have something like this:

Foo myObj = [s         


        
5条回答
  •  情书的邮戳
    2020-12-02 05:32

    You can set a conditional break point in Xcode by setting the breakpoint normally, then control-click on it and select Edit Breakpoint (choose Run -> Show -> Breakpoints).

    In the breakpoint entry, there is a Condition column.

    Now, there are several issues to keep in mind for the condition. Firstly, gdb does not understand dot syntax, so instead of myObj.name, you must use [myObj name] (unless name is an ivar).

    Next, as with most expressions in gdb, you must tell it the type of return result, namely "BOOL". So set a condition like:

    (BOOL)[[myObj name] isEqualToString:@"Bar"]
    

    Often it is actually easier to just do this in code by temporarily adding code like:

    if ( [myObj.name isEqualToString:@"Bar"] ) {
        NSLog( @"here" );
    }
    

    and then setting the break point on the NSLog. Then your condition can be arbitrarily complex without having to worry about what gdb can and can't parse.

提交回复
热议问题