Categories
Computing iphone Mac OS X

Private methods in Objective-C

Coming from the Java world, I love, care and embrace all things private, including private methods. Objective-C supports the feature but it appears to me (based on very shallow Googling) that there is not too much familiarity (or use) with it. I looked in a book I recently acquired, Learn Objective-C for Java Developers (Learn Series). While not a book a perfect book, it does cover the how-to of private methods in Objective-C.  

The key to the approach is to leverage Objective-C categories. Categories are a mind-blowingly nifty in the fact that they allow you, among other things, to augment objects you did not create with your own methods. While there are other ways of 'hiding' private methods, most of them result in warnings in Xcode. So here goes…

Say you have an object called CoolWidget. You create the header file (CoolWidget.h)for it as usual, i.e.:

@interface CoolWidget:NSObject
{
    int publicVar;

    @private
    int somePrivateVar;
}

// public methods
-(void) doSomething:(BOOL)cool;

@end

To add private methods to it, you would create a category for the object in a separate header file (CoolWidget+Private.h):

@interface CoolWidget (Private)

// private methods
-(void) doSomethingPrivate:(BOOL)cooler;

@end

All that's left for you to do is to include the additional header file, CoolWidget+Private.h in your implementation file, CoolWidget.m (though categories are often/normally implemented in their own .m file).

Update:

As my friend Glenn Barnett points out in his tweet, there is no such think as private methods in Objective-C. Unlike C++ or Java, there is no real 'enforcement' of access to the methods. Privacy, overall, is purely by convention. 

Share
Categories
Computing Java Web Development

Google App Engine: Brief First Impression

Started playing with the Google Apps Engine last night. Now that Java is supported, even I can be lured after the magical charm of what is close to the infinite scalability holy grail. These are my impressions spending a couple of hours with the tutorial:
1. Eclipse makes it easy
Google created a pretty nifty plugin for the latest generation of Eclipse to help you with app engine development. It generates some of the code you need and hides the boring stuff. It also contains a Jetty app server that helps you deploy apps locally for debugging.

2. Old Java, new tricks?
Google appears to want to simplify the uptake and adoption. Therefore the tutorial uses old school servlets and JSPs. Need to test deeper to see if there is any reason not to use more modern Sping framework and templating engines like Velocity. Doubt that.

3. It’s JDO, JPA: not your average ORM
This will take some getting used to. To gain access to the bounty of ‘infinite’ no headache (supposedly) storage you will need to get cozy with JDO or JPA. These mainstream Java standards are less popular than Hibernate and other ORM libraries. You will need to learn yet another framework. Had one nit here with an annotation specifying the type of primary key causing a bug.

Overall, though, things appeared polished and working. Cannot wait to play more.

Share
Share