I’m starting out on objective-C and so far I was under the belief that the .h and .m file should be in sync with-respect-to method details. However it seems I can add methods to the .m file without the addition of their sig in the .h file and it will still compile fine !
For e.g. this works in the .m file without any declaration in the AddressCard.h file.
-(BOOL) isEqual:(AddressCard *)theCard
{
if ([self.name isEqualToString:theCard.name]==YES &&
[self.email isEqualToString: theCard.email] ==YES)
return YES;
return NO;
}
-(NSComparisonResult) compareNames:(AddressCard *)theCard
{
return [self.name compare:theCard.name]; //default is ascending
}
Am’I missing something here. ??
You needn’t declare methods in the .h unless they’re going to be accessed by other classes (consider it your public API).
However, it’s worth noting that in the .m file, order matters. If you define -foo then define -bar later in the file, -bar can invoke -foo. However the compiler will complain if -foo tries to invoke -bar unless -bar is declared prior to -foo’s definition. This declaration could be in the .h file or simply earlier in the .m.