2

I come from a Delphi and .Net background and have just started iPhone development. I just came across a problem whilst debugging.

I had the following code:

if ([displayText rangeOfString:@"."].location != NSNotFound) .....etc

I wanted to evaluate this whilst I was debugging but could not work out how to do it. I found the Expressions window and entered the below but nothing happened:

[displayText rangeOfString:@"."].location

As I'm used to Delphi & .Net (and I know XCode is a different product) its very easy to stick in variables, methods etc into a watch window and then see the result but I cannot see how to do this in XCode. Please can you tell me how I evaluate things whilst debugging??

Thanks

2 Answers 2

2

In your case, what you would do is at the debugger is type:

p (NSRange)[displayText rangeOfString:@"."]

You can print out the value of objects with po, but things like C structures have to be printed out with "p" and you have to cast the return types from ObjC calls to the correct struct type.

Also, just putting this in the Expressions window should result in a value:

(NSRange)[displayText rangeOfString:@"."]

In both cases you will see the whole NSRange struct, with location and length.

Sign up to request clarification or add additional context in comments.

2 Comments

Is this the main way to find return values from methods etc?
Yes, mostly I use p (or po if the return type is an object) at the prompt. But you can also edit a breakpoint and add an expression to print, which you can use to automatically print out values at breakpoints - you can also tell breakpoints not to stop execution, so it can print and continue.
2

You can watch variables by going in the debugging drop down in the main menu on top and selecting watch variable. You can also right click and you should see the option "watch variable." Alternatively you can hover your mouse over the desired variable while stepping through your code to see its value at that time

5 Comments

How do you do it for my example?
Run (on the top menu), select Debugger from drop down. Then you should see the debugger window and a list of vars from your program. Right click var you wanna watch and you'll see the watch var option. Click on that to watch the variable (ie. you'll be told when var value changes etc)
You may wanna check out Staford's tutorial's on Xcode and debugging. They are concise and helpful for beginners
Thanks. How is that different from the top right window that shows arguments and locals in a treeview?
I don't think he was asking to see when a value changed; also it would not be possible to set a watch on an expression (at least not one dealing with a string location like that).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.