我是新手,只是想了解简单的概念。 我已经阅读了目标C for dummies和Cocoa Programming for Mac OSX(大部分内容)。 我试着自己制作一个简单的小程序,并意识到我知之甚少。
我一直收到"Use of Undeclared Identifier "calculateAge', did you mean 'Calculate' "
错误。
任何人都可以告诉我下面的代码有什么问题,为什么? 先谢谢了。
#import <Foundation/Foundation.h>
@interface Calculate : NSObject
{
int myYear;
int nowYear;
}
- (int) calculateAge:(int)birthYear:(int)nowYear;
@end
@implementation Calculate
- (int) calculateAge:(int)birthYear:(int)nowYear// need myYear
{
NSLog(@"The birthYear is: %i\n", birthYear);
int myAge = nowYear - birthYear;
//NSLog(@"The nowYear is: %i\n", nowYear);
NSLog(@"The age is: %i\n", myAge);
return myAge;
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSLog(@"Hello, World!");
int myY = 1963;
int nowY = 2012;
int myYear = 1963;
int nowYear = 2012;
//int myAge = calculateAge:(int) birthYear: (int) nowYear;
int myAge = calculateAge:(int) myY: (int) nowY;
NSLog(@"The nowYear is: %i\n", nowYear);
NSLog(@"The age is: %i\n", myAge);
}
return 0;
}
@end
答案 0 :(得分:1)
您似乎将方法名称与参数列表混为一谈。正确的签名看起来像这样:
- (int)calculateAgeFromBirthYear:(int)birthYear currentYear:(int)currentYear
然后可以这样实现:
- (int)calculateAgeFromBirthYear:(int)birthYear currentYear:(int)currentYear
{
NSLog(@"The birthYear is: %i\n", birthYear);
int myAge = currentYear - birthYear;
//NSLog(@"The nowYear is: %i\n", currentYear);
NSLog(@"The age is: %i\n", myAge);
return myAge;
}
然后可以这样调用:
Calculate *calculator = [[Calculate alloc] init];
int myAge = [calculator calculateAgeFromBirthYear:myY currentYear:nowY];
不要在类实现中实现main
;它属于全局命名空间外部,最好是在自己的文件中。
答案 1 :(得分:0)
无需在界面中声明实例变量
最后@end
需要在main函数之前,它不是类的一部分,它使用了类
需要实例化Calculate
类,并且需要对实例化的类进行调用
应重命名calculateAge...
方法以指示每个参数
变量应该被赋予全名,通常的缩写最终会使事情变得不那么明确。
以下是一个例子:
@interface Calculate : NSObject
- (int) calculateAgeWithBirthYear:(int)birthYear nowYear:(int)nowYear;
@end
@implementation Calculate
- (int) calculateAgeWithBirthYear:(int)birthYear nowYear:(int)nowYear// need myYear
{
NSLog(@"The birthYear is: %i\n", birthYear);
int myAge = nowYear - birthYear;
NSLog(@"The age is: %i\n", myAge);
return myAge;
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSLog(@"Hello, World!");
int myBirthYear = 1963;
int nowYear = 2012;
Calculate *calculator = [[Calculate alloc] init];
int myAge = [calculator calculateAgeWithBirthYear:myBirthYear nowYear:nowYear];
NSLog(@"The nowYear is: %i\n", nowYear);
NSLog(@"The age is: %i\n", myAge);
}
return 0;
}
NSLog输出:
你好,世界!
诞生年份是:1963年
年龄是:49
现在年份是:2012年
年龄是:49
使用`@ property~ statements:
创建的实例变量@interface Calculate : NSObject
@property int myYear;
@property int nowYear;
- (int) calculateAge;
@end
@implementation Calculate
- (int) calculateAge // need myYear
{
NSLog(@"The birthYear is: %i\n", self.myYear);
int myAge = self.nowYear - self.myYear;
NSLog(@"The age is: %i\n", myAge);
return myAge;
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSLog(@"Hello, World!");
Calculate *calculator = [[Calculate alloc] init];
calculator.myYear = 1963;
calculator.nowYear = 2012;
int myAge = [calculator calculateAge];
NSLog(@"The nowYear is: %i\n", calculator.nowYear);
NSLog(@"The age is: %i\n", myAge);
}
return 0;
}