2014-03-29 52 views
0

我在Xcode中有一個NSObject類和@implementation行給我一個錯誤; 未找到'initWithName:tutorID:tutorPhone:tutorEmail:'的方法定義。未找到'initWithName ...'的方法定義。錯誤

我.h文件中:

#import <Foundation/Foundation.h> 

@interface TutorsItem : NSObject 

@property (nonatomic, strong) NSString *tutorName; 
@property (nonatomic) int tutorID; 
@property (nonatomic, strong) NSString *tutorPhone; 
@property (nonatomic, strong) NSString *tutorEmail; 


- (id)initWithName:(NSString *)tutorName tutorID:(int)tutorID tutorPhone:(NSString *)tutorPhone tutorEmail:(NSString *)tutorEmail; 

@end 

我.m文件:

#import "TutorsItem.h" 

@implementation TutorsItem 

@synthesize tutorName = tutorName; 
@synthesize tutorID = tutorID; 
@synthesize tutorPhone = tutorPhone; 
@synthesize tutorEmail = tutorEmail; 

- (id)initWithName:(NSString *)title subtitle:(NSString *)subtitle { 
    self = [super init]; 
    if (self) { 
     self.tutorName = tutorName; 
     self.tutorID = tutorID; 
     self.tutorPhone = tutorPhone; 
     self.tutorEmail = tutorEmail; 

    } 

    return self; 
} 

@end 
+0

的定義或實現? – Wain

+0

錯誤是**定義** @Wain –

+3

爲什麼您的實現方法根本不匹配您的接口方法? – Wain

回答

0

只要你很清楚什麼人試圖告訴你在你的.h你的代碼

- (id)initWithName:(NSString *)tutorName tutorID:(int)tutorID tutorPhone:(NSString *)tutorPhone tutorEmail:(NSString *)tutorEmail; 

但在你的.m文件以下行你有這樣的...

- (id)initWithName:(NSString *)title subtitle:(NSString *)subtitle 

正如你可以看到兩行代碼不匹配,編譯器希望你擁有如您在.H定義的具有相同名稱的.m文件的方法。因此,要擺脫錯誤的.m文件需要有以下

- (id)initWithName:(NSString *)tutorName tutorID:(int)tutorID tutorPhone:(NSString *)tutorPhone tutorEmail:(NSString *)tutorEmail 
{ 
    self = [super init]; 
    if (self) { 
     self.tutorName = tutorName; 
     self.tutorID = tutorID; 
     self.tutorPhone = tutorPhone; 
     self.tutorEmail = tutorEmail; 

    } 

    return self; 
} 

正如其他人也指出,它不是一個好主意,只是你的參數名稱是一樣的性質,增加了混淆。這同樣適用於你的@synthesize

這...

@synthesize tutorName = tutorName; 

不妨是這樣...

@synthesize tutorName; 
+0

非常感謝。我對客觀的C有些新感謝,因爲感謝它讓我失望:)我不再在我的電腦上,但我會盡快嘗試。 –

0

Method defenition not found是不是一個錯誤,這是一個警告。你得到它,因爲你沒有在@implementation部分提供你的方法的定義。您仍然可以編譯並運行您的代碼,但如果您嘗試調用此方法,則您的應用程序將崩潰。只需提供定義和此警告將消失

而且順便說一句擺脫這樣的:

self.tutorName = tutorName; 
    self.tutorID = tutorID; 
    self.tutorPhone = tutorPhone; 
    self.tutorEmail = tutorEmail; 

- (id)initWithName:(NSString *)title subtitle:(NSString *)subtitle因爲你在那裏做什麼是沒有意義的。你在那裏做的是給自己分配變量值

0

報告不是一個錯誤,它是一個警告(除非你已經啓用「警告是錯誤」)。當你的@interface聲明你的@implementation沒有提供的方法時,這是一個常見的警告。

在@Wain註釋中,您的init方法在實現中的簽名非常不同;你打算讓它匹配你定義的簽名嗎,還是你打算提供兩個初始化程序?有一種方法不會出現在@interface中,這很好,但預計@interface中顯示的所有內容都是用於實現的。

+0

是的,這是一個警告,抱歉。但是,我該如何解決它? –

+0

要修復它...提供缺少的方法或更改接口定義以匹配您已有的方法:) – mah

+0

好吧,我發現了錯字。標題應該是tutorName,字幕是tutorPhone。這會解決它嗎? –

1

你想你的.m文件更改爲:

- (id)initWithName:(NSString *)tutorName tutorID:(int)tutorID tutorPhone:(NSString *)tutorPhone tutorEmail:(NSString *)tutorEmail{ 
    self = [super init]; 
    if (self) { 
     self.tutorName = tutorName; 
     self.tutorID = tutorID; 
     self.tutorPhone = tutorPhone; 
     self.tutorEmail = tutorEmail; 

    } 

    return self; 
} 

函數的頭在.m文件只包括字幕,而不是tutorName,tutorId等。 我希望這有助於:)

+0

順便說一句,我可能會改變你的@synthesize變量名或改變initWithName函數中的變量名,因爲它們發生碰撞。 –