Showing posts with label Objective C. Show all posts
Showing posts with label Objective C. Show all posts

Friday, August 29, 2014

A Macro to Dump CGRect

Recently, I worked on a class derived from UIScrollView.  In order to dump the CGRect's conveniently, I mean better than NSStringFromCGRect(), I introduced the following macro:

#ifdef DEBUG
    #define DBG(format, ...)                NSLog(@"%d,%s,"format,__LINE__,__FUNCTION__,##__VA_ARGS__);
    #define DUMP_RECT(rect)                 DBG("\n"                   \
                                                #rect"\n"              \
                                                "....x:|%.0f|\n"       \
                                                "....y:|%.0f|\n"       \
                                                "....width:|%.0f|\n"   \
                                                "....height:|%.0f|\n", \
                                                rect.origin.x,         \
                                                rect.origin.y,         \
                                                rect.size.width,       \
                                                rect.size.height)
#else
    #define DBG(format, ...)
    #define DUMP_RECT(rect)
#endif

Note the stringizing operator (#) operator to dump the "rect". Its sample use in LPTSCrollView class:

- (void)reset
{
...
    DUMP_RECT(self.frame)
    DUMP_RECT(self.bounds)
...
}

Its sample outputs:

2014-08-29 16:15:03.829 PhotonPro[18253:60b] 98,-[LPTScrollView reset],
self.frame
....x:|0|
....y:|0|
....width:|320|
....height:|568|
2014-08-29 16:15:03.830 PhotonPro[18253:60b] 100,-[LPTScrollView reset],
self.bounds
....x:|0|
....y:|0|
....width:|320|
....height:|568|

Hopefully, it will help somebody!


Monday, February 16, 2009

A Debug Macro for Xcode

* Updated on 28 November 2011 for Xcode 4.2

I am a newcomer to the Xcode and I decided to check the debug means. Basically, I was looking for a macro to dump line number, file name and function name along with some output. However, I found only some "simple" debug classes. I think debug output should be done by macros. I checked the Apple documentation and found an interesting topic: Variadic Macros. They are introduced by the C99 standard. After a few attempts I decided that the following macro is good enough for me:

//
//  dbg.h
//
//  Created by oguz.kupusoglu@gmail.com on 15/2/2009.
//  Copyright 2009 Oguz Kupusoglu. All rights reserved.
//

#ifdef DEBUG
    #define DBG(format, ...)                   NSLog(@"%d,%s,"format,__LINE__,__FUNCTION__,##__VA_ARGS__);
    #define DBG_CALLED                         DBG("called")
#else
    #define DBG(format, ...)
    #define DBG_CALLED
#endif

#define LOG(format, ...)                       NSLog(@"%d,%s,"format,__LINE__,__FUNCTION__,##__VA_ARGS__);
#define LOG_CALLED                             LOG("called")   

Note that DBG_CALLED is a helper define which is useful for following the callstack. I ignored the file name, i.e. __FILE__, as the method name being in the [class method] format gives a hint of the file in question due to naming files after the class names convention. Certainly one can add __FILE__ at the expense of extra overhead. Sample usage and output as received from the Console which is accessed from the "View/Debug Area/Activate Console" option of the Xcode are below:

-(void) viewWillAppear:(BOOL)animated
{
    DBG_CALLED
    
    DBG("view frame\n"
        "origin x:|%.0f|  y:|%.0f|\n"
        "width:|%.0f|  height:|%.0f|\n",
        self.view.frame.origin.x, self.view.frame.origin.y,
        self.view.frame.size.width, self.view.frame.size.height)   
       
    DBG("year:|%d|", 2009)
   
    DBG("updated")
       
    [super viewWillAppear:animated];
}

...] 69,-[MyViewController viewWillAppear:],called
...] 75,-[MyViewController  viewWillAppear:],view frame
origin x:|0|  y:|20|
width:|768|  height:|1004|
...] 77,-[MyViewController viewWillAppear:],year:|2009|
...] 79,-[MyViewController viewWillAppear:],updated

Note that the DEBUG define is set via the "PROJECT/Build Settings" option. Go to "... Preprocessing" section and add the define "DEBUG" to the "Debug" subsection.

Finally, I think "Product/Generate Output\Generate Preprocessed File" is a good option as it gives the preprocessor output of the selected file. For example the above DBG() macros are expanded as below:

-(void) viewWillAppear:(BOOL)animated
{
    NSLog(@"%d,%s,""called",69,__FUNCTION__);

    NSLog(@"%d,%s,""view frame\n" "origin x:|%.0f|  y:|%.0f|\n" "width:|%.0f|  height:|%.0f|\n",75,__FUNCTION__, self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);





    NSLog(@"%d,%s,""year:|%d|",77,__FUNCTION__, 2009);

    NSLog(@"%d,%s,""updated",79,__FUNCTION__);

    [super viewWillAppear:animated];
}

Note that when there is no variable argument, "##" removes the trailing comma! Just excellent...

Friday, November 21, 2008

Creating an Objective C Development Environment

For your information, I am using Ubuntu 8.04 and I intend to develop something for the iPhone in my spare time. Certainly I am at the very begining of a long learning curve!

First, I installed the GNU Objective C compiler. Next, I decided to set up a development environment. After a quick search I have found that GNUstep is what I need: "GNUstep is a cross-platform, object-oriented framework for desktop application development....GNUstep is generally compatible with the OpenStep specification and with recent developments of the MacOS (Cocoa) API...". Please see: http://www.gnustep.org/. I installed GNUstep via the Synaptic tool: I simply searched for "GNUstep" and then installed the relevant packages.

Then, I compiled an example form the following link:

http://www.gnustep.org/resources/documentation/Developer/Base/ProgrammingManual/manual_1.html

I created a directory named "objc" and created two text files shown below there:

source.m:

#include

/*
* The next #include line is generally present in all Objective-C
* source files that use GNUstep. The Foundation.h header file
* includes all the other standard header files you need.
*/
#include

/*
* Declare the Test class that implements the class method (classStringValue).
*/
@interface Test
+ (const char *) classStringValue;
@end

/*
* Define the Test class and the class method (classStringValue).
*/
@implementation Test
+ (const char *) classStringValue;
{
return "This is the string value of the Test class";
}
@end

/*
* The main() function: pass a message to the Test class
* and print the returned string.
*/
int main(void)
{
printf("%s\n", [Test classStringValue]);
return 0;
}

GNUmakefile:

include $(GNUSTEP_MAKEFILES)/common.make

TOOL_NAME = LogTest
LogTest_OBJC_FILES = source.m

include $(GNUSTEP_MAKEFILES)/tool.make

In order to setup the GNUstep "environment", one of the following scripts should be run:

/usr/share/GNUstep/Makefiles/GNUstep.sh
/usr/share/GNUstep/Makefiles/GNUstep.csh

Note that the paths are installation specific. As I used bash and don't bother to maintain user settings being the only user, I added the following lines to my bash global settings file:

/etc/bash.bashrc:

# Oguz Kupusoglu @ 20 Nov 2008
. /usr/share/GNUstep/Makefiles/GNUstep.sh

Then I launched a bash shell and make the build which was a piece of cake:

..objc$ make
This is gnustep-make 2.0.2. Type 'make print-gnustep-make-help' for help.
Making all for tool LogTest...
Compiling file source.m ...
Linking tool LogTest ...

In order to run the application I swicthed to the "obj" subdir:

..objc$ ls
GNUmakefile obj source.m
..objc$ cd obj
..objc/obj$ ls
LogTest source.d source.o
..objc/obj$ ./LogTest
This is the string value of the Test class

That is all! Now you have a working Objective C development environment.