0

I'm 2 weeks into learning Objective-C (but previous experience with Java and Python), and after the initial hurdle I'm starting to get a hang of things like pointers, ARC, and delegates. However, on more than one occasion I've come across a problem/bug that I have no idea how to solve, and worse, I have no idea how to approach a solution.

My general troubleshooting strategy, for when things aren't working as expected, is as follows:

  1. Reread through the relevant section of my code to make sure the general logic and flow makes sense
  2. Look at any self-defined methods and make sure they're working properly
  3. Put a few NSLog statements within the code to see where it is doing unexpected things
  4. Once I've identified the troublesome part(s), search the apple docs/stackoverflow/google to see if anyone has experienced the same problems.

I might be omitting a step , but this general process works for the most part. However, there are some problems that I come across for which this process does not work, and I get totally stuck. A few examples:

  • I was trying to load an NSWindowController using the proper method called in the proper location, but it wasn't displaying. Turned out to be an issue with pointers, after a long time of aimless experimenting
  • I'm testing an example pulled directly from an Apple Technical Q&A to create a clickable hyperlink into an NSAttributedString, but when you hover over said link the pointing hand cursor does not appear. See my thread for more info.
  • I've created an NSButton and want the pointing hand cursor to appear when I hover over the button. I'm using the code in the accepted answer here, but it doesn't work and I don't know why. Also, subclassing every button I want to use would be tedious and messy.

Clearly my troubleshooting strategy is not working. Once I exhaust all 4 steps to no avail, I have to just try random fixes until one, seemingly magically, works. This is neither time efficient nor good for helping me understand the language better. Are there steps that I can add to my troubleshooting strategy, and if so, what are they?

For example, when I asked a friend the same question, they suggested using breakpoints to monitor instance variables and objects.

Community
  • 1
  • 1
Connor
  • 533
  • 4
  • 12
  • Never underestimate the usefulness of simple print statements. – Tom Zych Jun 25 '14 at 10:32
  • You need to improvise ;) Start using Breakpoints instead of `NSLog`. And w.r.t to Xcode, there are some very useful debugging tools which you have not mentioned. **Exception Breakpoints**: When your app is about to crash it halts at the line which is throwing an exception. **Zombie Objects** (although not needed now as frequently in non ARC times) When turned on Objects will not get released when reference count becomes 0 and any future calls made on this object ends up in `unrecognised selector sent to instance` Usefull to find out memory leaks. – GoodSp33d Jun 26 '14 at 02:53
  • Thanks @GoodSp33d -- I did not know about Exception Breakpoints and Zombie objects, I will be sure to check them out. On a separate note, I'm curious what I can do to my question in order to make it less broad so it complies with StackOverflow's standards. The answers I'm trying to get are those such as yours (GoodSp33d). I suppose that's broad, but if I narrowed down the question to the point where it's "how do I fix this 1 bug", it will cease being useful to me. – Connor Jun 26 '14 at 18:34

1 Answers1

0

When you get across a bug,

  1. first thing you need to do is read the error, see what the error is and where it occurred.
  2. Go to that specific location of the error, see if there is an obvious mistake.
  3. If not, set up a break point at skeptical location, then step in and check step by step to see if each value on the stack is what you expected it to be.
  4. You may want to put up some test, consider all possibilities, such as what if this value is null, what if it's 0, etc.

Sometimes bugs are tricky, but is not really recommended to read your code all over again each time you see an error.

Anton
  • 559
  • 2
  • 15