Philipp Fritschi
Philipp Fritschi

Reputation: 1

outsourcing code

My iPhone game has a lot of recurring code (move pictures, add score), that makes it too big when repeating the same code on each button click.

this is ViewController.m

interface and implementation between Viewcontroller.h and ViewController.m is correct - workes well

- (IBAction)button_xx_pressed:(id)sender
{

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

}

so I tried to make outsourced recurring code. I don't need method or functions that gives a result back or something. Just do some action like NSLog output...(just a test). Or in the original version - move pictures, add score and other stuff.

this is Outsourcing.h

#import "Outsourcing.m"

@end

this is Outsourcing.m

#import "Outsourcing.h"


- (void)this_is_a_test {

    int test_variable = 999;

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

}


@end

this would shrink the size of my game more than 80% (very important). I have thousands of recurring programming lines and at the moment, I don't know how to handle it.

actual error messages:

Outsourcing.h => missing context for method declaration

Outsourcing.m => missing context for method declaration => @end must appear in Objective-C context

Anyone any hints for me? Thank you very much... The rest of my game is ok... everything would run without issues. I'm very glad that I got it running (but the game size is a problem). 1 or 2 months ago, I never used xcode before. I just had some experience in VBA. And what I want is similar to.

=> Call this_is_a_test

=> Private Sub this_is_a_test()

But it seems I'm too stupid :-(

thanks

Upvotes: 0

Views: 195

Answers (2)

khose
khose

Reputation: 763

You are missing

@interface Outsourcing : NSObject

in your header file (Outsourcing.h). Remove:

#import "Outsourcing.m"

You import header files, not source files....You are also missing:

@implementation Outsourcing

In your .m file, just after the import declaration.

Upvotes: 0

Daniel
Daniel

Reputation: 577

@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

and you call it like this in your 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];  
}

Upvotes: 1

Related Questions