类创建无法识别的选择器

时间:2014-05-08 08:35:01

标签: objective-c macos class unrecognized-selector

我正在学习Objective-C。 我正在创建一个简单的类,但在编译时,在预期的结果之后,我得到了这个错误。

2014-05-08 10:15:47.998 ClassCreation[978:903] This is a method call in the new object!
2014-05-08 10:15:48.007 ClassCreation[978:903] -[Employee setName:]: unrecognized selector sent to instance 0x10010c850
...
terminate called after throwing an instance of 'NSException'
(gdb) 

这是Employee.h

#import <Foundation/Foundation.h>
@interface Employee : NSObject

// properties
@property NSString *name;
@property NSDate *hireDate;
@property int employeeNumber;

-(void) someMethod;

@end

这是Employee.m

#import "Employee.h"

@implementation Employee

// matching the method definition that is defined in the interface
-(void) someMethod {
NSLog(@"This is a method call in the new object!");
}

@end

这是main.m

#import <Foundation/Foundation.h>
#import "Employee.h"

int main (int argc, const char * argv[])
{

@autoreleasepool {

    //  inside here, I instantiate a new object of my new employee class
    Employee *fred = [[Employee alloc] init];

    // call a method of our new object
    [fred someMethod];

    // change a property of new object
    [fred setName:@"Fred Smith"];

}
return 0;
}

我使用Xcode 4.2进行10.6.8

1 个答案:

答案 0 :(得分:0)

使用XCode 4.2,您需要添加行

@synthesize name,hireDate,employeeNumber;

到@implementation行后的Employee.m文件

自动合成属性仅适用于XCode 4.4 - http://useyourloaf.com/blog/2012/08/01/property-synthesis-with-xcode-4-dot-4.html

您应该考虑升级您的XCode版本