0
#import "PsychologistViewController.h"
#import "HappinessViewController.h"

@interface PsychologistViewController()
@property (nonatomic) int diagnosis;
@end

@implementation PsychologistViewController

@synthesize diagnosis = _diagnosis;

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{
       if ([segue.identifier isEqualToString:@"ShowDiagnosis"]) {
        [segue.destinationViewController setHappiness:self.diagnosis];
       } 
       else if ([segue.identifier isEqualToString:@"Celebrity"]) {
        [segue.destinationViewController setHappiness:100];
       } 
       else if ([segue.identifier isEqualToString:@"Serious"]) {
        [segue.destinationViewController setHappiness:20];
       } 
       else if ([segue.identifier isEqualToString:@"TV Kook"]) {
        [segue.destinationViewController setHappiness:50];
       }
}

****- (void)setAndShowDiagnosis:(int)diagnosis****
{
    self.diagnosis = diagnosis;
    [self performSegueWithIdentifier:@"ShowDiagnosis" sender:self];
}

-(IBAction)flying
{
    [self setAndShowDiagnosis:85];
}

-(IBAction)apple 
{    
[self setAndShowDiagnosis:100];
}

-(IBAction)dragons
{
    [self setAndShowDiagnosis:20];
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

@end

My question pertains to the - (void)setAndShowDiagnosis:(int)diagnosis method. This method is undeclared anywhere as either public in any .h file and obviously it's not there privately either. My question is why the reason for this would be? It just shows its setter implementation but the actual method declaration appears nowhere. Any help to clarify this is appreciated. Oh and this is from an online lecture and everything compiles just fine and runs.

3 Answers3

1

Methods do not need to be declared, publicly, privately, or otherwise. Declaring a method in a .h file gives other users of the class knowledge of those methods. By not declaring it, you are hiding that method from the rest of the program that is using the class.

Dan F
  • 17,654
  • 5
  • 72
  • 110
  • +1. The point of the interface declaration is that it defines the interface of the class. What you'd call "private methods" in C++ or Java are not part of the interface, so there's no reason they have to be part of the interface declaration. (In C++, they have to be there so the compiler can statically resolve function calls and build vtables; in Java they have to be there because there is no separate declaration and definition; in ObjC, neither of those applies.) – abarnert Jun 13 '12 at 20:31
0

This method is undeclared anywhere as either public in any .h file and obviously it's not there privately either.

I think you answered your own question, its coming up undeclared, because it isn't being declared. Unless I am reading this wrong?

WhoaItsAFactorial
  • 3,538
  • 4
  • 28
  • 45
0

You don't have to declare methods if you want them to private. There is no such thing as private methods in objective-c.

The difference between declaring a method in the header file, and a class extension at the top of implmentation file, is that if you don't declare it in the header, and you use the method from another class then the compiler will warn you that the method may not exist. But as long as you've implemented the method the application will not crash and the method will be called.

You could get away with not declaring any methods anywhere, but you will get lots of compiler warnings and it is harder to read later on, and harder for other people to understand your code. And there will be greater chance of you causing a crash because you miss spelt a method name,or some other trivial mistake.

Jonathan.
  • 53,997
  • 54
  • 186
  • 290
  • It's not true that "there is no such thing as private in objective-c". There is very definitely such a thing, it just doesn't apply to method declarations. – abarnert Jun 13 '12 at 20:33
  • Private really doesn't apply in obj-c methods though, you can still attempt to call a method on an object even if it isn't declared up front, like Jonathan said, you'll just get a compiler warning. – Dan F Jun 13 '12 at 20:41
  • Sorry, abarnert you are a correct. I just missed out the word "methods" – Jonathan. Jun 13 '12 at 21:12