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

ankakusu picture ankakusu · Nov 14, 2013 · Viewed 30.2k times · Source

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

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

Answer

Adam Wright picture Adam Wright · Nov 14, 2013

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];

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