Successful debugging of a program begins with one major rule: verify your assumptions.
That is, you may believe your code successfully does A B and C.
However, upon closer inspection, you realize that instructions A and B complete successfully, but C does not.
How do you get to this point, however? That is, with NimbleKit-based iOS applications — how do you identify instruction C as the culprit?
In an ideal world, we’d be able to use modern debugging tools — namely, to be able to step through the code line by line, inspect variable values, set breakpoints, et cetera. Sadly, while Xcode fully supports this type of debugging — due to the Javascript-based nature of NimbleKit — we cannot use Xcode’s excellent debugging tools.
What else can we use?
Option 1 – Alert box dialogs
The first option — easiest for quick and dirty inspection of variable values — we can use NKAlert.
Example — not sure that a parameter is being received from a page? You could use code like this:
var passedID = NKGetParameter(“nid”);
NKAlert(“alert”, “passedID is ” + passedID); // DEBUG
The second line of code will cause an alert dialog to pop up, allowing you to see if passedID actually contains a value.
However, using anything more than one alert dialog can get tedious — what if you need to check the value of three variables? Five? The flood of alert dialogs very quickly gets annoying, and prevents you from “normally” navigating within your application.
Option 2 – Writing to a log file
Second option — best for intensive debugging — and for the ability to cut and paste information being logged — NKLog.
A common problem is a complex SQL query that is not running properly (causing, for example, your table view to display no data).
If you use an NKAlert line, you can “see” the query as it will be run by NimbleKit — for example:
var query = “UPDATE notes ” +
“SET ” +
“title = ‘” + title + “‘, ” +
“body = ‘” + body + “‘, ” +
“lastmodified = ‘” + dateTimeStamp + “‘ ” +
“WHERE id = ” + passedID;
NKAlert(“alert”, query); // DEBUG
The problem is that, for the query, while visually inspecting it can help — it’d be better to actually be able to cut and paste the query into Base, and verify that the query works and is returning the data you expect.
How to do this? NKLog to the rescue. Watch this video, and follow along to get the full details:
How to Use NKLog for Debugging with NimbleKit
Basically, to get NKLog to work with your own project, add these two lines to “main.m” in the “Other Sources” folder of your Xcode project:
// Modify the first argument of the next line to reflect the path to your own Desktop
freopen( “/Users/rgordon/Desktop/stderr.txt”, “w”, stderr );
Then modify the first argument of the second line to reflect the actual path to your own Desktop folder. (Again — the video has all the details).
Good luck and happy debugging!