簡體   English   中英

iOS錯誤:'xxxx'沒有可見的@interface聲明選擇器'alloc'

[英]iOS error: No visible @interface for 'xxxx' declares the selector '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函數已在基類中定義。 那么為什么程序會出現這樣的錯誤呢?

請注意,我已經檢查了這個主題,但它對我不起作用。

我的通靈調試器,沒有讀取你的代碼,告訴我你在一個對象實例而不是一個類上調用alloc alloc方法是由類(通常繼承自NSObject )定義的靜態方法,它返回基礎對象的新實例。 你不能要求實例分配自己!

現在看代碼,我看到你想要:

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

構造一個新實例,並將其分配給myValidator屬性。

更換

[[self.myValidator alloc] init];

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

該錯誤表示您尚未為self.myValidator實現alloc實例方法,這是真的。 但這是一個適用於所有NSObject對象的類方法。

您創建對象的語法不正確。 正確的代碼:

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

對於其他:檢查varible名稱一樣的類名。 好吧,它發生在我身上。

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

不要像我現在那樣告訴任何人。

對於那些得到錯誤"no visible @interface for declares the selector ..."

如果您輸入的方法名稱輸入錯誤,或者該方法根本不屬於該類且在您的類中不存在,則通常會發生此類錯誤

我今天遇到了這個問題並且自己解決了。 基本上你也可能無法滿足功能/程序的所有要求。

進入課堂本身,確保你宣布所有的要求。

我把這個類從標題庫中取出並逐字逐字比較,以驗證它與使用它的函數匹配。

如果您經常隨意地體驗(比如當您更換分支時),而不是因為您忘記聲明選擇器。

轉到文件檢查器 > 目標成員身份

  1. 取消選中目標
  2. 然后再檢查一下

文件檢查員

這將刷新project.pbxproj

然后,如果你建立,你會看到你真正的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM