代码之家  ›  专栏  ›  技术社区  ›  Johann Burgess

“initWithName…”的方法定义未找到。错误

  •  0
  • Johann Burgess  · 技术社区  · 10 年前

    我在Xcode中有一个NSObject类 @实施 线路给了我一个错误; 未找到“initWithName:tutorID:tutorPhone:tutorEmail:”的方法定义。

    我的.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
    
    4 回复  |  直到 10 年前
        1
  •  1
  •   Yotam Vaknin    10 年前

    您希望将.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等。 我希望这有助于:)

        2
  •  0
  •   Flexicoder    10 年前

    只是为了让你清楚人们在你的中试图告诉你什么。你有以下代码行

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

    但是在你的.m文件中你有这个。。。

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

    正如您可以看到的那样,这两行代码不匹配,编译器希望您在.m文件中有一个方法,其名称与您在.h中定义的名称相同。因此,要消除此错误,.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;
    
        3
  •  0
  •   mah    10 年前

    报告不是错误,而是警告(除非您启用了“警告是错误”)。当您的@interface声明了您的@implementation没有提供的方法时,这是一个常见的警告。

    在@Wain comments,您的实现中有一个签名非常不同的init方法;您是否希望它与您定义的签名匹配,或者您是否计划提供两个初始值设定项?有一个方法不出现在您的@接口中是很好的,但您的@界面中呈现的所有内容都应该有一个实现。

        4
  •  0
  •   Andrey Chernukha    10 年前

    Method defenition not found 不是错误,而是警告。你得到它是因为你没有在里面提供方法的定义 @implementation 部分您仍然可以编译和运行代码,但如果尝试调用此方法,应用程序将崩溃。只需提供定义,此警告将消失

    顺便说一句:

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

    在…内 - (id)initWithName:(NSString *)title subtitle:(NSString *)subtitle 因为你在那里做的事毫无意义。您所做的是为自己分配变量值