xcode 未声明的标识符目标 C
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13554839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Undeclared identifier Objective C
提问by ukbaz
I can't seem to get round this error; use of undeclared identifier 'ageBy'. I dont understand why am getting it as i have the import Person.h in my code. Thanks for your time and any help.
我似乎无法解决这个错误;使用未声明的标识符“ageBy”。我不明白为什么要得到它,因为我的代码中有导入 Person.h。感谢您的时间和任何帮助。
Person.h
人.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
人.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
回答by Tommy
Your ageBy:
is defined inside summaryString
. You probably want to move the curly bracket just before @end
so that it is above -(void)ageBy:(int)years
. So:
你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;
}
Also as a style note, if summaryString
is merely for debugging then you'd possibly be better off declaring it as description
. The latter is the standard form for getting an implementation-dependand string description of an Objective-C object, with the net effect that collection objects like NSArray
know to call description
on all their child objects in order to create the correct output.
同样作为样式说明,如果summaryString
仅用于调试,那么最好将其声明为description
. 后者是获取 Objective-C 对象的依赖于实现的字符串描述的标准形式,其最终效果是集合对象NSArray
知道调用description
其所有子对象以创建正确的输出。
回答by rmaddy
As was stated, the problem is caused by the ageBy:
method being embedded in the summaryString
method.
如前所述,问题是由ageBy:
方法中嵌入的summaryString
方法引起的。
I wanted to demonstrate how this class could be written using modern Objective-C:
我想演示如何使用现代 Objective-C 编写此类:
// Person.h
@interface Person : NSObject
@property (nonatomic, assign) int age;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *job;
- (void)ageBy:(int)years;
@end
// Person.m
@implementation Person
- (NSString *)description {
return [NSString stringWithFormat:@"The Person %@ is %d years old and is a %@", self.name, self.age, self.job];
}
- (void)ageBy:(int)years {
self.age = self.age + years;
}
@end