• 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
Guarding against missing ObjC implementation
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: Guarding against missing ObjC implementation
      • From: Damien Bobillot <email@hidden>
    • Re: Guarding against missing ObjC implementation
      • From: Daniel Jalkut <email@hidden>
  • Prev by Date: Radix Class
  • Next by Date: Re: NSAttributedString: lossy attachments when serializing/archiving?
  • Previous by thread: Re: Solutions for "mocking" out NSUserDefaults?
  • Next by thread: Re: Guarding against missing ObjC implementation
  • Index(es):
    • Date
    • Thread