2 starwar2012 starwar2012 于 2012.11.27 14:28 提问

Objective C中没有声明的标示符

实在是不知道该怎么绕过这个错误,总是说:use of undeclared identifier 'ageBy'。请高手帮我指点一下吧,先看看我的代码:

Person.h

@interface Person : NSObject
{
 int _age;
 int _years;
 NSString *_name;
 NSString *_job;

} 

-(void)setAge:(int)age;
-(int)age;

-(void)setName:(NSString *)name;
-(NSString *)name;

-(void)setJob:(NSString *)job;
-(NSString *)job;

-(NSString *)summaryString;

-(void)ageBy:(int)years;


@end

Person.m

#import "Person.h"
@implementation Person

-(void)setAge:(int)age{
  _age = age;
}
-(int)age{
  return _age;
}
-(void)setName:(NSString *)name{
  _name = name;
}
-(NSString *)name{
  return _name; }

-(void)setJob:(NSString *)job{
  _job = job;
}
-(NSString *)job{
  return _job;
}

-(NSString *)summaryString{
  return [NSString stringWithFormat:@"The Person %@ is %d years old and is a  %@",_name,_age,_job];

-(void)ageBy:(int)years{
  _years = years;
  _age = years + _age;

 }

 } 
 @end

1个回答

LPPloveROU
LPPloveROU   2012.11.27 14:54
已采纳

你的ageBy是在summaryString中定义的,应该移动{}括号到@end之前,这样它就在
-(void)ageBy:(int)years中了,

-(NSString *)summaryString{
  return [NSString stringWithFormat:@"The Person %@ is %d years old and is a  %@",_name,_age,_job];
 } 

-(void)ageBy:(int)years{
  _years = years;
  _age = years + _age;

 }
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!