外包代码

时间:2012-10-14 14:13:36

标签: objective-c

我的iPhone游戏有很多重复代码(移动图片,添加分数),这使得在每次点击按钮时重复相同代码时太大了。

这是ViewController.m

Viewcontroller.h和ViewController.m之间的接口和实现是正确的 - 工作得很好

- (IBAction)button_xx_pressed:(id)sender
{

    //trying to call the outsourced code
    [self this_is_a_test];  

}

所以我试图制作外包的重复代码。我不需要返回结果的方法或函数。只需做一些像NSLog输出的动作......(只是一个测试)。或者在原始版本中 - 移动图片,添加分数和其他内容。

这是Outsourcing.h

#import "Outsourcing.m"

@end

这是Outsourcing.m

#import "Outsourcing.h"


- (void)this_is_a_test {

    int test_variable = 999;

    NSLog(@"Give me a test output: = %i", test_variable);

}


@end

这会使我的游戏规模缩小80%以上(非常重要)。我有成千上万的重复编程线,目前,我不知道如何处理它。

实际错误消息:

Outsourcing.h =>缺少方法声明的上下文

Outsourcing.m =>缺少方法声明的上下文               => @end必须出现在Objective-C上下文

对我有任何暗示吗?非常感谢...我的游戏的其余部分都没问题......一切都会毫无问题地运行。我很高兴我让它运行(但游戏大小是一个问题)。 1或2个月前,我从未使用过xcode。我刚才有过VBA的经验。我想要的是类似的。

=>请致电this_is_a_test

=> Private Sub this_is_a_test()

但似乎我太傻了: - (

感谢

2 个答案:

答案 0 :(得分:1)

@interface Outsourcing : NSObject

- (void)this_is_a_test;

@end

#import "Outsourcing.h"

@implementation
- (void)this_is_a_test {

    int test_variable = 999;

    NSLog(@"Give me a test output: = %d", test_variable);
}
@end

你在ViewController中这样称呼它:

#import "Outsourcing.h"

...

- (IBAction)button_xx_pressed:(id)sender
{
    Outsourcing *outsourcing = [[[Outsourcing alloc] init] autorelease];

    //trying to call the outsourced code
    [outsourcing this_is_a_test];  
}

答案 1 :(得分:0)

你错过了

@interface Outsourcing : NSObject

在头文件(Outsourcing.h)中。删除:

#import "Outsourcing.m"

您导入头文件,而不是源文件....您也缺少:

@implementation Outsourcing

在你的.m文件中,就在导入声明之后。

相关问题