Objective-C错误“"XYZPerson"的无可见@interface声明选择器"saySomething" [英] Objective-C error "No visible @interface for 'XYZPerson' declares the selector 'saySomething'

查看:87
本文介绍了Objective-C错误“"XYZPerson"的无可见@interface声明选择器"saySomething"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Objective-C真的很陌生,当我练习本书练习时,我真的被困在这里.请帮我解决这个问题,并且三个多小时以来,我一直在想什么可能导致此错误.还是我没明白!

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.

提前谢谢!

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

让它正常工作!感谢@MatthewD,@trojanfoe和@JFS的大力帮助:)

推荐答案

(从注释移至答案...)

MatthewD:如果将XYZPerson.h中的- (void) saySomething;更改为- (void) saySomething:greet;,会发生什么?

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

Raj0689:为什么将其更改为saySomething:greet而不是saySomething时,它可以运行?由于只与saySomething !!

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.

因此,请在XYZShout.m的呼叫位置:

So, in XYZShout.m where you call:

[super saySomething:upperGreet];

编译器搜索由XYZShout.m导入的XYZShout.h和由XYZShout.h导入的XYZPerson.h.在XYZShout.h中,发现了以下方法:

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;

这在名称上匹配被调用的方法,但在参数上不匹配,因此编译器不认为这是匹配项.在任何地方都找不到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.

这篇关于Objective-C错误“"XYZPerson"的无可见@interface声明选择器"saySomething"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆