我刚开始学习Objective-C和OOP ...所以我有非常愚蠢的问题:) 我想了解如何在Objective-C中编写递归函数。如何用类对Objective-C中的递归进行编码?
我以阶乘计数为例。这里是我的代码))他,他
#import <Foundation/Foundation.h>
@interface Factorial : NSObject
{
int nFact;
}
-(int) countFactorial:(int) nFact;
-(void) printFactorial;
@end //Factorial
@implementation Factorial
-(int) countFactorial:(int) n
{
int tmp;
if (n!=0)
{
tmp=n*countFactorial(n-1);
}
else {
return(0);
}
nFact=tmp;
}
-(void) printFactorial
{
NSLog(@"Factorial = %i",nFact);
}
@end //Factorial
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
id myFact=[Factorial new];
int qqq=[myFact countFactorial:5];
[myFact printFactorial];
[myFact release];
[pool drain];
return 0;
}
我从编译器收到错误 - 我隐声明countFactorial功能 ,我得到链接错误(当然),它没有找到countFactorial
Plz给我的任何建议如何在Objective-C 中实现递归我想在我的下一个“项目”中使用它 - tic tac toe - 通过递归找到正确的移动)
Thx!