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

这是我的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

这是我尝试在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

当我尝试编译代码时,我收到以下错误

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

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

请注意,我已经检查了this主题,但它对我不起作用.

解决方法

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

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

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

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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...