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
General

Snowmass

Share
Categories
Computing Consumer

You get what you paid for: Inland Pro Wireless 2.4 Ghz Optical Mouse and Keyboard Combo

One of my Christmas vacation goals was to set up an old
desktop computer as a media center device (running Boxee). More on
that when I’m done with the effort. To control the machine I was
looking for an affordable wireless keyboard and mouse solution. I
found a cheap Inland combo at MicroCenter. It uses RF so it should
be able to work with the barely hidden computer. So I thought. And
for $20 it’s a deal. Well, you get what you pay for. On looks alone
the keyboard and mouse are handsome and the USB transmitter is of
the cool tiny kind, and barely visible. Touching the devices gives
the right impression. Cheap and flimsy. To get to the tiny
transmitter you need to remove the battery cover from the mouse and
pull it out of a dedicated compartment. Quite nifty except for the
fact that the mouse cover is flimsy and does not pull off gently.
Eventually everything was in place. What makes this a bad accessory
is that it fails to do either of its jobs. The keyboard barely
manages to send key strokes to the computer. Even a 4 year old
typing with a single finger was too fast. And that’s when the key
strokes even registered. Normally something was off with the
connectivity. Changing batteries did nothing to improve matters.
The mouse did not fare any better. It did not ‘wake’ from wait
state until it was moved roughly and even then lost reception. Not
very helpful. Clearly this goes back to the store. Since it did not
harm the machine it is not the worst. But useless overall.

Share
Share