Question

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.

Was it helpful?

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

OTHER TIPS

First of all, you should really take a look at the basic Objective-C documentation.

In Objective-C, a method can be preceded by a + or - sign.

+ is for class methods, - is for instance methods.

Then you have the return type, inside parenthesis, and the method name.

- ( int )foo;

An instance method named foo, returning an int.

A similar C function would be:

int foo( void );

In Objective-C, the method name is a bit special when you have arguments.

For instance:

- ( int )foo: ( double )num;

A member method named foo:, returning an int and taking a double argument named num.

Similar C function:

int foo( double num );

Now with multiple arguments:

- ( int )foo: ( double )num1 bar: ( float )num2;

A member method named foo:bar:, returning an int and taking a double argument named num1 and a float argument named num2.

Similar C function:

int foo( double num1, float num2 );

About your question on id, it's simply the method return type.

id is a typedef used for Objective-C instances.
Basically, it's a void *.

id does represent an Objective-C object pointer, for any class.

First the dash (-) in the method name says that this is an instance method which means you need an instance to send this message to. The call would look something like this:

NSString* s = @"a string"; [someInstance addOwnersNameObject:s];

In this case you are passing the NSString instance s to the addOwnersNameObject message.

id is like void * in C.

To add to those very valid answers already given with a further discussion of id:

Objects in Objective-C are typeless, which means that at a fundamental level you don't need to know the type to be able to talk to the object. That's one of the big differences between Objective-C and, say, C++.

Pointers to objects are usually typed, such as NSString * to make the code more readable and to indicate your intentions to the compiler so that it can provide suitable warnings if you do anything odd.

id is a typeless pointer to an object. Any object type can be passed as id and any id value can be assigned to any object pointer without casting.

99.99% of the time, id could be replaced with NSObject * since 99.99% of objects inherit from NSObject, meaning that you could use the fact of inheritance rather than the fact of typeless objects to pass things around generically. However NSObject is a little bit special in being both an object and a protocol and some objects aren't actually subclasses of NSObjectNSProxy and the classes that represent blocks jump immediately to mind. You'll rarely be particularly interested in those special cases but id is nevertheless often used as a convention because people prefer the semantics of passing an object with no indication of its type to passing an object with a known ancestor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top