iPhone Development

Just another iPhone Blog

Tag Archives: objective c

Capturing Exceptions while debugging

This one is a simple but could be time-saving 🙂

Scenario: If you are debugging and caught with awkward situation where the application crashes with an exception, like NSInvalidArgumentException and so on, but never take you to the place in the code where the problem exists.

 

Solution:

Remember, This solution is for Xcode 4.2.1 for Lion.

1.  Select View->Navigators->Show Breakpoint Navigator

Screen Shot 2012 02 22 at 4 42 58 PM

2. At the Bottom-left cornet, Click the ‘+’ symbol and Select ‘Add Exception Breakpoint’ Menu

Screen Shot 2012 02 22 at 4 43 20 PM

3. In the popped-up window you may select [All | Objective-C | C++ ] options to select what type of Exception should the execution stop.

Screen Shot 2012 02 22 at 4 44 15 PM

 

4. Click Done.

Restart your debug process and you should see the line of code that is causing the hassle for you 🙂

 

Courtesy: Apple Developer Document.

 

 

Building Objective-C static libraries with categories

I just came across the usage of Linker flag -all_load and -ObjC

Basically, this is used in situations where you build a static library with Categories. When this static library is linked with the actual implemenation / application that use this library, -ObjC linker flag would inform the compiler to load all the Objective C classes implementation.

However, since there is a linker bug that prevents -ObjC from loading objects files from static libraries that contain only categories, we should use -all_load or -force_Load Linker flags.

For More Details. See Apple Technical QA QA1490

Quick Tip : Initializing static variables of a class in Objective C

Initializes the receiver before it’s used (before it receives its first message).

 

+ (void)initialize


Discussion The runtime sends initialize to each class in a program exactly one time just before the class, or any class that inherits from it, is sent its first message from within the program. (Thus the method may never be invoked if the class is not used.) The runtime sends the initialize message to classes in a thread-safe manner. Superclasses receive this message before their subclasses.


[Reference : NSObject Class Reference]

You can place all your static member variable initialization code in this method as it guarantees it would be called only once BEFORE any method in that Class is invoked through any of its objects.

Happy Coding 🙂