Help Please with Objective C

Discussion in 'Mac Programming' started by MasterObiWan, Jan 12, 2009.

  1. MasterObiWan macrumors newbie

    Joined:
    Jan 12, 2009
    #1
    I'm faced with the task to learn a new language: Objective C. In the past I have learned languages like Java and Visual Basic with it's abundances of developer resources I learned these languages swiftly like riding the waves. So here I am it's 4 o clock in the morning, lecture at 9, havent started my report which is due tommorow and I'm trying to figure out why I can't call a method. what the **** am i doing wrong?


    My error: 'someMethod' undeclared (first use in this function)

    The method was written in viewController.m,
    Code:
    #import "MemoPadViewController.h"
    
    @implementation MemoPadViewController
    
    - (IBAction)done
    {
    	[textView resignFirstResponder];
    	[userInput resignFirstResponder];
    }
    
    - (IBAction)showText
    {
    	NSInteger addedNum;
    
    	
    	[B]addedNum = someMethod:10;[/B]<----the problem
    
    	addedNum = [someMethod 10]; <--this does not work either
    }
    
    - (int)someMethod:(int)xArg
    {
    	return 5;
    }
    
    
    What does it take to call a method in Objective C? I've thought it must have something to do with file im writing it in or I might need to make an instance? Can someone please point me out on where I should be going.

    I wouldn't have to ask this question if Apple hadn't chosen a language so little used, with it's esoteric documentation it will drive any beginner programmer insane to the point where he will give up on developing for Apple. It's no wonder Microsoft has 90% of the market share.

    Note: and yes I've googled for hours and can't anything.
     
  2. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #2
    It takes a receiver and a method. In your case, the receiver should be "self", since you are calling one of your own class's methods. So the syntax
    Code:
    addedNum = [self someMethod: 10];
    
    ought to do the trick.
     
  3. GorillaPaws macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #3
    I know you may be a bit cash-strapped as a student, but the Stephen Kochan book "Programming in Objective-C 2.0" is a really great resource for learning Objective-C. His 2nd edition is hot off the presses and worth picking up if you are taking a full semester on the language.
     
  4. MasterObiWan thread starter macrumors newbie

    Joined:
    Jan 12, 2009
    #4
    I got it to work, thanks. I still got another error though but all I needed to do was place the method before I called it.

    for other newbs who might stumble upon this page:
    <CODE>
    On Dec 3, 2008, at 1:38 PM, Søren Krogh Neigaard wrote:
    So it seems somehow it can not identify my method, even though it is in the same class. Im a Java person, what I really wanted is just a private method here??

    Ken gave a good answer, but to elaborate on some differences between Objective-C and Java methods:

    * Unlike in Java, you can't declare a method anywhere and have the whole class know about it. The compiler has to have seen the method either declared or defined before you call the method, otherwise you'll get warnings.

    * In Objective-C, you typically have separate declaration and definition for a method that you want to advertise. The declaration goes in the .h file, and the definition (implementation) goes in the .m file. This is unlike Java where there is no such thing as a separate declaration.

    * In Objective-C, methods can't be truly private, only unadvertised. You can send any message you want to any object you want (possibly generating a compiler warning at worst), and at runtime if the object implements that method, regardless of any attempts to hide this fact, the method will be called.

    --Andy
    </CODE>
     

Share This Page