Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
538 views
in Technique[技术] by (71.8m points)

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

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 = [self gimmeObj];

myObj might have a property called name. I want the debugger to stop on the assignment when

[myObj.name isEqualToString:@"Bar"];

How can I set my conditional breakpoint in Xcode to do that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...