Search code examples
objective-cmethodsinstance-method

Objective C Instance Method Help *Beginner*


Can you guys help me understand a concept real quick, I'm having trouble understanding the conversion from C to objective-C:

If I had a particular instance method that look like this:

-(void)addOwnerNamesObject:(NSString *)n; 
{
    // ownerNames defined as NSMutableSet
    [ownerNames addObject:n];
}

I understand a few things...

  1. It is an instance method that can be called by the program.
  2. In C this would not return anything (just execute the code in the curlies)
  3. In C, the syntax is slightly less confusing - (void)InstanceMethod(Char *nameOfArgument)

Here's where I need help:

  1. When you call this method are you still sending it an argument?
  2. If so, is that argument an NSString instance that the method names n?

And finally... off topic

If you have a method...

-(id)someMethod:(NSString *)pn
{
}
  1. What is the (id) for? does that tell the compiler that it can return any type of object?

Thanks for helping the Newbie... Much appreciated.


Solution

  • You already know what you're talking about.

    1.) When you call this method are you still sending it an argument?

    yes, whatever is after the colon

    add multiple colons to pass additional parameters...

    -(void)addOwnerNamesObject:(NSString *)n withSomeIntYouWantToPass:(int)value;

    2.) If so, is that argument an NSString instance that the method names 'n'?

    yes

    3.) What is the (id) for? Does that tell the compiler that it can return any type of object?

    yes, you will return an NSObject or subclass of NSObject