Search code examples
objective-ccocoadebuggingnslog

ObjC - Problems with using an NSLog replacement?


Being relatively new to programming--think Programming in Objective-C by Kochan, Chapter 15--I'm wondering if there's a reason why it's a bad idea--especially for a new programmer?--to use an NSLog replacement such as the following:

#define MGLog(format, ...) CFShow([NSString stringWithFormat:\
    format, ## __VA_ARGS__])

and then call it as follows:

MGLog(@"Yo' mama wears combat boots.");

It's much cleaner to use this for learning how to manage strings, building a rolodex program, like he's got me doing, but I don't want to get into the habit of doing it this way if there are drawbacks. Thanks for any help, guys & gals.

BTW, if it matters, I'm using XCode 4.


Solution

  • Well, there is ABSOLUTELY no problem with that.

    The purpose of log functions is to provide you with as much (useful) information as possible.

    If you think that using THIS version of NSLog for debugging is more helpful to you, then how could it be wrong?


    Note : The more involved you become with Objective-C/Cocoa program, the more likely it is that you'll set to one or the other log function (perhaps, one you'll write yourself to suit your particular needs).

    Here's the one I'm mostly using :

    #define _LOG(prefixch, fmt, ...) \
          NSLog((NSString*)(CFSTR("%c [%s:%d] " fmt)), prefixch, \
                __SRC_FILENAME__, __LINE__, ##__VA_ARGS__)
    

    It shows the FILE we're in, the LINE we're at and anything else I might need...