iOS错误:'xxxx'没有可见的@interface声明选择器'alloc' [英] iOS error: No visible @interface for 'xxxx' declares the selector 'alloc'

查看:1108
本文介绍了iOS错误:'xxxx'没有可见的@interface声明选择器'alloc'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的TextValidator类:

Here is my TextValidator class:

//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

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

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

//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类继承自NSObject,据我所知,init和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?

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

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

推荐答案

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

My psychic debugger, without reading your code, tells me you're calling alloc on an object instance, rather than a class. The alloc method 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!

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

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

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

构建新实例,并将其分配给 myValidator property。

to construct a new instance, and assign it to the myValidator property.

这篇关于iOS错误:'xxxx'没有可见的@interface声明选择器'alloc'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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