21 post tagged

iOS

Later Ctrl + ↑

Simple debug and release logging

I like to use NSLog() in my apps for simple debugging and controlling application flow.

There’s a slightly better way, which heeds the DEBUG flag and won’t use NSLog() if the product is packaged for release.

In your App-Prefix.pch, add the following lines and use DLog() for “debug” logging and ALog() for “always” logging just as you would have used NSLog(). In this way, your DLog() statements won’t make it into release.

#ifdef DEBUG
#   define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#else
#   define DLog(...)
#endif

// ALog always displays output regardless of the DEBUG setting
#define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)

There are better solutions, but this is a simple one that works.

 No comments   2014   iOS

StackOverflow links for documentation

Just recently Shaun Inman wrote:

I thought, sure it’s not good at all as documentation, but on the other hand it’s amazing how StackOverflow now has the answers to a lot of problems you may encounter while programming. Mostly basic things, but it’s not bad: better spend time on hard problems, than getting something simple straight which doesn’t work for some reason. In this case StackOverflow usually has a working solution.

Just wanted to write that it’s just Objective-C, which is pretty niche (only Apple hardware), but to my surprise, according to April’s TIOBE index, Objective-C snatched third place from C++, having gained 3.5%.

On a sidenote, I recently started reading many gamedev folks, and it’s a lot of fun.

 No comments   2014   iOS

On date formatting

My app shows due dates, of course I need proper date formatting. Unfortunately NSDateFormatterStyle is not enough: it won’t let you specify anything about how you want your date to be displayed.

Yet there’s a class method on NSDateFormatter called dateFormatFromTemplate:options:locale: which gives you total control. The fun (and the most useful) thing about it is that you just need to give it components of a date, and it will arrange the format string automatically based on locale. Neat.

Don’t forget to do setDoesRelativeDateFormatting:NO, otherwise you’ll just get a blank string on the output. If you do need relative formatting at the same time with custom date format strings, here you go: NSDateFormatter+RelativeDateFormat.[hm]. I used this as a base for my own customization.

 No comments   2014   Coursera Due   iOS
Earlier Ctrl + ↓