Guarding against missing ObjC implementation
Guarding against missing ObjC implementation
- Subject: Guarding against missing ObjC implementation
- From: Daniel Jalkut <email@hidden>
- Date: Mon, 9 Jan 2006 12:11:06 -0500
One of the nicest features and worst pitfalls of Objective-C
programming is the dynamic messaging nature of the language.
In the "pitfall" category is a problem I run into from time to time
where a project possesses the required header file for a particular
category, but somehow the source file corresponding to that header
has been omitted from the project.
When this happens, the compile triggers no warning: you don't get
"incomplete class" warnings when a category's methods are not
fulfilled at compile/link time.
I'd like to guard against this problem biting me in the future, and
I'd like your feedback. What is the best approach?
One approach would be to add unit tests for every category. I put
something like this in one of my test suites as a practical experiment:
------------------------
#define RequireClassMethod(myClass, mySel) \
do { \
NSString* description = [NSString stringWithFormat:@"Check for %@
in %@", NSStringFromSelector(mySel), NSStringFromClass([myClass
class])]; \
STAssertTrue([myClass instancesRespondToSelector:mySel],
description); \
} while (0)
- (void) testRequiredMethodsPresence
{
// NSString+FileUtils
RequireClassMethod(NSString, @selector(revealInFinder));
RequireClassMethod(NSString, @selector(openFileInFinder));
}
------------------------
This does the trick, but adding explicit tests for every method
implemented by a category seems overly laborious.
Has anybody got any clever tricks for avoiding the whole class of
"dynamic functionality may carelessly disappear" problems? This
particular aspect of Objective-C makes it very scary to reorganize/
refactor projects. I'm pretty-well used to the dynamic nature of the
language by now, but most developers are, I think, conditioned to
trust the linker to find these types of errors.
Daniel
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden