iOS 错误:“xxxx”没有可见的@interface 声明了选择器“alloc”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19974682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:55:51  来源:igfitidea点击:

iOS error: No visible @interface for 'xxxx' declares the selector 'alloc'

iosobjective-calloc

提问by ankakusu

Here is my TextValidator class:

这是我的 TextValidator 类:

//TextValidator.h
#import <Foundation/Foundation.h>

@interface TextValidator : NSObject
- (BOOL) isValidPassword:(NSString *)checkPassword;
- (BOOL) isValidEmail:(NSString *)checkString;
- (BOOL) isEmpty:(NSString *)checkString;
@end 


//  TextValidator.m
#import "TextValidator.h"

@implementation TextValidator

- (BOOL) isEmpty:(NSString *)checkString
{
    return YES;
}

- (BOOL) isValidPassword:(NSString *)checkPassword
{
return YES;
}

- (BOOL) isValidEmail:(NSString *)checkString
{
return YES;
}

@end

This is the way I try to initialise the TextValidator class in ViewController.m:

这是我尝试在 ViewController.m 中初始化 TextValidator 类的方式:

//ViewController.h
#import <Foundation/Foundation.h>

@interface SignUpViewController : UIViewController <UITextFieldDelegate>
@end

//ViewController.m
#import "SignUpViewController.h"
#import "TextValidator.h"

@interface SignUpViewController ()

@property TextValidator *myValidator;

@end

@implementation SignUpViewController

- (void)viewDidLoad
{
    [[self.myValidator alloc] init]; //iOS error: No visible @interface for 'TextValidator' declares the selector 'alloc'*
    [super viewDidLoad];
}
@end

When I try to compile the code I get the following error:

当我尝试编译代码时,出现以下错误:

No visible @interface for 'TextValidator' declares the selector 'alloc'.

TextValidator class inherits from NSObject and as far as I know init and alloc functions are already defined at the base class. So why does the program gives such an error?

TextValidator 类继承自 NSObject,据我所知 init 和 alloc 函数已经在基类中定义。那么为什么程序会出现这样的错误呢?

Note that, I already checked thistopic and it doesn't work for me.

请注意,我已经检查了这个主题,它对我不起作用。

回答by Adam Wright

My psychic debugger, without reading your code, tells me you're calling allocon an object instance, rather than a class. The allocmethod is a static method defined by classes (typically inherited from NSObject) that returns a new instance of the underlying object. You can't ask an instance to allocate itself!

我的通灵调试器没有阅读你的代码,就告诉我你正在调用alloc一个对象实例,而不是一个类。该alloc方法是由类(通常从 继承NSObject)定义的静态方法,它返回底层对象的新实例。你不能要求一个实例分配自己!

Now looking at the code, I see that you want:

现在看代码,我看到你想要:

self.myValidator = [[TextValidator alloc] init];

to construct a new instance, and assign it to the myValidatorproperty.

构造一个新实例,并将其分配给myValidator属性。

回答by Danut Pralea

Replace

代替

[[self.myValidator alloc] init];

with

self.myValidator = [[TextValidator alloc] init];

The error signals that you have not implemented the allocinstance method for self.myValidator, which is true. But that's a class method that applies for all NSObjectobjects.

错误信号表明您尚未实现 的alloc实例方法self.myValidator,这是真的。但这是一个适用于所有NSObject对象的类方法。

回答by Vladimir

Your syntax of creating object is incorrect. Correct code:

您创建对象的语法不正确。正确代码:

self.myValidator = [[TextValidator alloc] init]; 

回答by user1105951

For Others : Check the varible name is notlike the class name. Well it happend to me.

对于其他人:检查变量名与类名一样。好吧,它发生在我身上。

XXXViewController * XXXViewController = [[XXXViewController alloc] init];

Don't tell anyone like I did right now.

不要像我现在那样告诉任何人。

回答by Honey

For those who get the error of "no visible @interface for declares the selector ..."

对于那些得到错误的人 "no visible @interface for declares the selector ..."

such an error usually happens when you have mistyped the name of the method, or that method doesn't belong to that class at all and doesn't exist in your class

当您输入错误的方法名称,或者该方法根本不属于该类并且不存在于您的类中时,通常会发生此类错误

回答by Robert William Clancy

I had this problem today and solved it on my own. Basically you could also not be satisfying all the requirements of the function / procedure.

我今天遇到了这个问题,并自己解决了。基本上,您也无法满足功能/过程的所有要求。

Go into the class itself and make sure your declaring all the requirements.

进入课程本身并确保您声明了所有要求。

I took the class out of the header library and compared it word for word to verify it matches the function using it.

我从头库中取出类并逐字比较它以验证它是否与使用它的函数匹配。

回答by Ted

If you experience this randomly (like when you are changing branches), not because you forgot to declare selector.

如果您随机遇到这种情况(例如更改分支时),不是因为您忘记声明选择器。

Go to file inspector> Target Membership

转到文件检查器>目标成员资格

  1. uncheck the targets
  2. then check it again
  1. 取消选中目标
  2. 然后再检查一次

File Inspector

文件检查器

This will refresh your project.pbxproj

这将刷新您的 project.pbxproj

Then if you build, you'll see your real problem

然后如果你构建,你会看到你真正的问题