Skip to content

Comment on First Glance at Objective-C and the iPhone SDK (by a Java and C Developer)

Comments

I started about a month ago... As someone with a background in Java/C++/Python, here are some more that have caught my attention:

- The ability to assign types as ClassName<Protocol>, instead of the protocol specifying the type completely. Neat, I guess...

- Because it's message passing, you get duck typing for free, albeit with compiler warnings (you send a message, and wouldn't you know it, the selector exists!). Not that I recommend this approach over protocols.

- The awesomeness of @selector, especially if you're coming from Java and used to writing anonymous classes everywhere simply for the sake of making functions first-class entities.

- The lack of public/protected/private visibility modifiers and how variables are suddenly part of the public API when you define them in properties. Say you have a variable you want to keep private, but you need to frequently update its value. You go ahead and define @property (nonatomic, retain) for it so within the class so that you can simply do self.varName = newVarValue to update it. But now all your clients/outside clients will see; you need to write the release-and-retain setter yourself in the .m file to prevent this.

- The minimalism of Xcode. Hey, it's faster than Eclipse, thank $DEITY for that. But when I want to alphabetize the filenames in the left sidebar, I have to go to Edit -> Sort -> By Name for _every_ file group?

You can define a category within your implementation file (.m) and redeclare the property like this:

@interface MyClass (PrivateMethods)

@property(readwrite, retain) NSObject *myProperty;

@end

@implementation MyClass ...

Now only your implementation knows about the generated setter methods, and you don't have to implement them yourself.

You can use @public, @private, or @protected on the instance variables. Unfortunately these do not extend to the properties themselves and you have to use a different approach as in the sibling comment:

  @interface SomeClass (SomePrivateCategory)
     @property (nonatomic, assign) NSUInteger count;
     ...
  @end

Actually, Objective-C has class extensions for just this purpose. No need for a private category.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.