-->

Objective-C error \"No visible @interface for '

2019-02-21 02:14发布

问题:

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 this error for more than three hours. Still I didn't get it!

Best regards, Raj.

Thanks in advance !

main.m

#import <Foundation/Foundation.h>
#import "XYZPerson.h"
#import "XYZShout.h"
int main(int argc, const char * argv[])
{

    @autoreleasepool {

        //XYZPerson *some = [[XYZPerson alloc]init];

        XYZShout *some = [[XYZShout alloc]init];
        [some sayHello];



        // insert code here...
       // NSLog(@"Hello, World!");

    }
    return 0;
}

XYZPerson.h

#import <Foundation/Foundation.h>
@interface XYZPerson : NSObject

@property NSString *firstName;
@property NSString *secondName;
@property NSDate *dob;

-(void) saySomething;
-(void) sayHello;

@end

XYZPerson.m

#import "XYZPerson.h"
@implementation XYZPerson

-(void) sayHello {
    [self saySomething:@"Hello all"];
}

-(void) saySomething:(NSString *)greet {
    NSLog(@"%@", greet);
}

@end

XYZShout.h

#import "XYZPerson.h"

@interface XYZShout : XYZPerson

// -(void) saySomething;

@end

XYZShout.m

#import "XYZShout.h"

@implementation XYZShout

-(void) saySomething:(NSString *)greet {
    NSString *upperGreet = [greet uppercaseString];
    [super saySomething:upperGreet];    // this is where I get the error mentioned above
}

@end

Got it working ! Thanks to @MatthewD , @trojanfoe , @JFS for your big help :)

回答1:

(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.



回答2:

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


回答3:

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