Re: Guarding against missing ObjC implementation
Re: Guarding against missing ObjC implementation
- Subject: Re: Guarding against missing ObjC implementation
- From: Damien Bobillot <email@hidden>
- Date: Tue, 10 Jan 2006 09:55:55 +0100
Daniel Jalkut wrote :
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?
The approach I personally use is to create a protocol describing what
methods must be implemented for a given functionality :
@protocol MyProto
- (void) revealInFinder;
- (void) openFileInFinder;
@end
Then, you may check if an object implementes all methods of this
protocol :
id object;
if([object conformsToProtocol:@protocol(MyProto)]) {
...
--
Damien Bobillot
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.
_______________________________________________
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