Objective-C error "No visible @interface for 'XYZPerson' declares the selector 'saySomething'

前端 未结 3 1525
半阙折子戏
半阙折子戏 2021-01-16 13:36

I am really new to Objective-C and when I was practicing the book exercises, I really am stuck here. Please help me solve this and I have been thinking what could cause thi

相关标签:
3条回答
  • 2021-01-16 14:21

    It looks like you are testing inheritance so I will assume that XYZShout is supposed to be derived from XYZPerson. If so follow the suggestion from @JFS and make sure it does actually derive:

    XYZShout.h:

    #import <Foundation/Foundation.h>
    #import "XYZPerson.h"
    
    @interface XYZShout : XYZPerson
    
    - (void)saySomething:(NSString *)greet;
    
    @end
    

    And also correct the definition of saySomething in XYZPerson (you missed off the parameter):

    XYZPerson.h:

    #import <Foundation/Foundation.h>
    @interface XYZPerson : NSObject
    
    @property NSString *firstName;
    @property NSString *secondName;
    @property NSDate *dob;
    
    - (void)saySomething:(NSString *)greet;
    //                   ^^^^^^^^^^^^^^^^^
    - (void)sayHello;
    
    @end
    
    0 讨论(0)
  • 2021-01-16 14:25

    Please make sure to set the XYZShout.h interface to @interface XYZShout : XYZPerson?

    0 讨论(0)
  • 2021-01-16 14:29

    (Moved from comments into an answer...)

    MatthewD: What happens if you change - (void) saySomething; in XYZPerson.h to - (void) saySomething:greet;?

    Raj0689: Why does it run when I change it to saySomething:greet and not saySomething ? Since greet is defined only along with saySomething !!

    When you call a method, the compiler needs to locate the signature of that method so it can verify that the method is being called correctly. The signature includes the method name and the number and types of parameters. The usual way of providing method signatures is by importing the header file that defines those signatures.

    So, in XYZShout.m where you call:

    [super saySomething:upperGreet];

    The compiler searches XYZShout.h, which is imported by XYZShout.m, and XYZPerson.h, which is imported by XYZShout.h. In XYZShout.h, the following method was being found:

    -(void) saySomething;

    This matches the called method in name, but not in parameters, so the compiler does not consider this a match. No other definitions of saySomething are found anywhere, so it gives an error instead.

    0 讨论(0)
提交回复
热议问题