Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Fritzables

macrumors regular
Original poster
May 6, 2011
149
1
Brisbane AUSTRALIA
Hi to all,

Is it possible to call a method from within another in the same Class?

I have code that will be repetitive that will be called by a number of methods within the same class. Instead of rewriting the same code when needed, I'd like to call on one method to do the job.

I have experimentad but when I go to Build I see an error indicating it is not defined - how do you define a method within the class it's in??

Hope this makes sense.

Pete
 
Hi to all,

Is it possible to call a method from within another in the same Class?

I have code that will be repetitive that will be called by a number of methods within the same class. Instead of rewriting the same code when needed, I'd like to call on one method to do the job.

I have experimentad but when I go to Build I see an error indicating it is not defined - how do you define a method within the class it's in??

Hope this makes sense.

Pete
It's really hard to figure out what exactly you are asking, but based on what I think you are asking, yes but the exact semantics of what you want to do depend on what language you are using. For example in Java the order you declare the methods doesn't matter, the compiler parses the method list before it starts compiling the code. For C-based languages you either need to declare the method in the header file, above the calling methods, or create a prototype for it.
 
Ummm...

Code:
@implementation MyClass

- (void)privateMethod  // Need to be before method1 & method2
{
}

- (void)method1
{
    [self privateMethod];
}

- (void)method2
{
    [self privateMethod];
}

@end

Is this what you mean?
 
If you don't want to have to declare your private method above your public ones (sometimes I don't want to do this as I quite like the methods in my implementation to match the order in the interface so the private ones go at the bottom) then you can use a category to implement your private methods:

MyClass.h
Code:
@interface MyClass
{
int myVar
}

-(void) myMethod;
@end

MyClass_Private.h
Code:
@interface MyClass(Private)
-(void) setupMyVar;
@end

MyClass.m
Code:
#import "MyClass.h"
#import "MyClass_Private.h"

@implementation MyClass

-(void) myMethod
{
[self setupMyVar];
}

-(void) setupMyVar
{
myVar = 1;
}
@end

Note the above was typed straight into this reply box: it may well have some errors but you can hopefully get the idea!
 
Last edited:
Hey Robbie, you forgot the parentheses around the return types. :p Perhaps you can edit your post to fix it, for posterity. -- Jim
 
Hey Robbie, you forgot the parentheses around the return types. :p Perhaps you can edit your post to fix it, for posterity. -- Jim

Doh! I knew something looked wrong. I've spent all morning writing T-SQL so my mind is not in the right place for Objective-C :p
 
Hi to all,

Is it possible to call a method from within another in the same Class?

I have code that will be repetitive that will be called by a number of methods within the same class. Instead of rewriting the same code when needed, I'd like to call on one method to do the job.

I have experimentad but when I go to Build I see an error indicating it is not defined - how do you define a method within the class it's in??

Hope this makes sense.

Pete

If I'm not mistaken, I think you can use the "self" keyword to access any method within another method in the same class.
 
Good morning All,

Thanks a lot for this. I am still on the 'L-Plates' coming from Visual Studio.
Yes, the key was if fact 'self'. As soon as that was in place, away it went.

I'm usng Objective-C and it is the closest I can get to C# which I used to use under Windows.

I am into my 5th week of Mac and already forgetting how to do things under Windows-7 (which I thought I'd never do) :D

At first I thought Xcode and Objective-C was so unlearnable but now - boy, is it powerful stuff, and I am still learning. Gut feel, I wouldn't mind betting you can be more productive under this enviroment than under Billie's. :)

Anyway, thanks again.

Pete
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.