0

我是客观的新手,只是想理解简单的概念。我已经阅读了 Objective C for dummy 和 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
4

2 回答 2

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在类实现中实现;它属于全局命名空间之外,最好是在它自己的文件中。

于 2012-10-17T17:29:06.763 回答
0

无需在接口中声明实例变量。
最后一个@end需要在主函数之前,它不是类的一部分,它使用了类。
该类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~ 语句创建的实例变量:

@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;
}
于 2012-10-17T17:34:29.020 回答