CodeEval在一个主.m文件中接受objective-c程序,但我的答案依赖于使用辅助类来执行我定义的递归方法调用。帮助程序类有一个单独的.h和.m文件,所以我总共有3个文件(包括我的主.m文件)。如何将它们作为一个文件一起提交?
我的第一个想法是将方法所做的工作转移到main,但我不能这样做,因为其中一个是递归的,需要能够调用自己。我似乎无法在objective-c中定义类之外的方法。
我不想将我的程序重新编写为main,而不需要重复,因为那样会很糟糕。
这是对CodeEval的objective-c提交的限制还是有其他方法可以做到这一点?
答案 0 :(得分:2)
为什么不把所有内容放在main.m中,如下所示:
#import <Foundation/Foundation.h>
// previously in myrecursivecomputer.h
@interface MyRecursiveComputer : NSObject
- (int)fac:(int)x;
@end
// previously in myrecursivecomputer.m
@implementation MyRecursiveComputer
-(int)fac:(int)x
{
if (x == 0) return 1;
else return [self fac:x-1] * x;
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
MyRecursiveComputer *c = [MyRecursiveComputer new];
int f = [c fac:4];
NSLog(@"Result: %d", f);
}
return 0;
}