• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Guarding against missing ObjC implementation
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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
References: 
 >Guarding against missing ObjC implementation (From: Daniel Jalkut <email@hidden>)

  • Prev by Date: Re: Crash in _blinkCaretTimerAction (NSTextView)
  • Next by Date: Re: Unable to Load Window NIB
  • Previous by thread: Re: Guarding against missing ObjC implementation
  • Next by thread: Setting values in InfoPlist.strings from Info.plist;
  • Index(es):
    • Date
    • Thread