RachelD
RachelD

Reputation: 4089

How to automatically verify all methods are declared in xcode

I am working with Objective-C in Xcode. I was wondering is there any mechanism to proof your code to make sure all functions and methods are declared in the .h file or in the private @interface method?

To clarify I will be on a coding tangent and will write a method directly in my viewController.m file

- (Awesome*) generateAwesomeOfMagnitude:(NSFloat)magnitude { ...

and I will forget to add the heading to the viewController.h file. If this is a private method nothing notifies me that I've done this so I have to go back through and verify that everything was declared manually when Im done. Is there any way to check automatically?

Note: Im looking to make the complier to throw a warning. Is there a setting is really what I should have asked.

Upvotes: 2

Views: 193

Answers (2)

isaac
isaac

Reputation: 4897

Omar's answer is correct - asking an object if it will respond to a selector is the preferred method for probing objects to see if they'll respond to a method at runtime. However, the question asks "How to automatically verify all methods are declared?" (presumably at compile time). And the answer is, short of writing something yourself, you cannot automatically do this.

This is part of what makes Objective-C 'dynamic'. You don't have to declare a method anywhere. This makes things possible like:

id anUnknownObject = [[NSClassFromString(whoKnowsWhatIllBe) alloc] init];
[anUnknownObject performSelector:@selector(whoKnowsWhatIllDo)];

This means, for example, you could fetch a string from a web service and instantiate a class based on the string alone (of course the class must be around at run time in order to be instantiated, but the compiler doesn't have a clue).

This doesn't mean you should program this way, but it means its possible, and, as with most things, there are appropriate use cases, and it's a great distinction of the language. It promotes extreme decoupling, polymorphism, and tons-o'-fun patterns.

It's generally regarded as best practice to declare private methods in .m class extensions, but the value of this is for the programmer, not the compiler. Some (to include a major contributor to Objective-C who shall go nameless in public forums for the time being) have also suggested that there isn't a need to type the names of all your methods twice in a single file (less code, less mistakes -a bit more scrolling if you're looking at someone else's class for the first time). Having a nice-n-tidy public API is exactly what the header is for. Having a clean implementation is important, but the assumption is that once you're in the m, you're in private territory anyway. Scroll around. See what the method names are.

Upvotes: 3

Omar Abdelhafith
Omar Abdelhafith

Reputation: 21221

To check if method exists at run time in the class use

if([yourObject respondsToSelector:@selector(generateAwesomeOfMagnitude:)])
    //has this method
else
    //does not have this method

but you dont get any warning if you define a method in the .m and dont include it in the .h file

However if another class is accessing a method that is not declared in the .h file you will receive a warning

Upvotes: 0

Related Questions