Calling a Method within a Class?

Discussion in 'Mac Programming' started by Fritzables, Jun 22, 2011.

  1. macrumors regular

    Joined:
    May 6, 2011
    Location:
    Brisbane AUSTRALIA
    #1
    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
     
  2. macrumors 6502a

    Joined:
    Jan 15, 2007
    #2
    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.
     
  3. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #3
    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?
     
  4. robbieduncan, Jun 22, 2011
    Last edited: Jun 22, 2011

    Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    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!
     
  5. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #5
    Hey Robbie, you forgot the parentheses around the return types. :p Perhaps you can edit your post to fix it, for posterity. -- Jim
     
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    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
     
  7. macrumors 6502

    Joined:
    Jan 13, 2011
    Location:
    Los Angeles, CA
    #7
    If I'm not mistaken, I think you can use the "self" keyword to access any method within another method in the same class.
     
  8. thread starter macrumors regular

    Joined:
    May 6, 2011
    Location:
    Brisbane AUSTRALIA
    #8
    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
     

Share This Page