Bad dynamic side of Objective-C

Objective-C is dynamic language. Although it doesn’t add many things to C, it is more dynamic than C++.
However, I declare most of objects statically. One reason is that static typing gives faster performance. However another reason, which is big, is that I can rely on the compiler’s syntax checking to reduce any errors caused by typo and so.

But sometimes you need to rely on its dynamic side.
Take a look at the following code.

@implementation JAConnection
- (id)initWithFileHandle:(NSFileHandle *)fileHandle delegate:(id)delegate
	if( self = [super init] )
		mFileHandle = [fileHandle retain];
		mDelegate = [delegate retain];

		isMessageComplete = YES;

		[self displayConnectedMOSInfo];

		[[NSNotificationCenter defaultCenter] addObserver:self
		[mFileHandle readInBackgroundAndNotify];

	return self;

#pragma mark Notification Messages
- (void)dataReceivedNotification:(NSNotification *)notification


It causes errors. Can you see which part is wrong? Xcode 3.x editor set a recognized token’s color differently, so you can get help from the editor.
But I didn’t see it still.

One big problem of this is that the source code is compiled and actually it worked. But somehow the dataReceviedNotification is not invoked.
Because there can be some causes in other part of its whole codes, I took a look at them carefully and couldn’t find why.

The reason was that addObserver’s selector argument.
It is written as “dataReceiveNotificaiton”. The correct one is “dataReceivedNotification”

I usually don’t use respondsTo method of NSObject. I know what types are expected and what type I use. So, I am trying not to use dynamic side of the Objective-C as much as possible.  But sometimes you need to rely on it like the example.

So, I have a habit to copy and paste method name rather than to check typos.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: