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

7

6

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.

Yield answered 14/11, 2013 at 10:2 Comment(0)
A
16

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.

Admittedly answered 14/11, 2013 at 10:4 Comment(0)
D
9

Replace

[[self.myValidator alloc] init];

with

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

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

Downtoearth answered 14/11, 2013 at 10:4 Comment(0)
B
1

Your syntax of creating object is incorrect. Correct code:

self.myValidator = [[TextValidator alloc] init]; 
Bearden answered 14/11, 2013 at 10:4 Comment(0)
F
1

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

File Inspector

This will refresh your project.pbxproj

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

Frankel answered 9/9, 2016 at 5:50 Comment(0)
E
1

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

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

Don't tell anyone like I did right now.

Eyeglass answered 25/1, 2018 at 18:40 Comment(0)
H
0

For those who get the error of "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

Hedjaz answered 26/1, 2016 at 17:25 Comment(0)
P
0

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.

Porterporterage answered 31/8, 2016 at 16:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.