PDA

View Full Version : Compiler Warnings: id and *id, multiple declarations




Jordan72
Dec 3, 2005, 09:53 PM
What syntax do I need so the XCode compiler accepts both id and id* types without giving me warnings? Although the compiler warnings don't turn into exceptions that stop my program in it's tracks or change my program logic, I heard it's good practice to remove all warnings and want to remove the warnings.


-method:(id)

-method:(id*)


I tried both versions by passing them both id and id* and my program worked fine, but how do I remove warnings with better syntax?


Since I'm on the topic of removing warnings, I can't seem to get rid of these kinds of warnings either.

I've only declared -(NSString *)set once:

[[sets objectAtIndex:i] omega:[[[sets objectAtIndex:i] set] length]-1];

warning: multiple declarations for method `set'


I never did declare -(int)length, so I don't know why it doesn't know what the heck is going on, since -(int)length is an NSString method.

[self possible:[self possible] * [[[sets objectAtIndex:i] set] length]];

warning: multiple declarations for method `length'



robbieduncan
Dec 4, 2005, 03:19 AM
You can't. A method in an ObjC class cannot be overloaded like it can in C++. You can't do what you are trying to do.

Why do you want to be able to pass id* anyway? id is already a pointer (it's declared as void* or obj_class* anyway).

Jordan72
Dec 4, 2005, 06:55 PM
I don't know what overloading is, but what I did describe up there worked fine, just got warnings.

Why id*? For the same reason of NSArray method:

- (id)initWithObjects:(id *)objects count:(unsigned)count

I'm merely following established conventions. We both want pointers to objects for our own purposes.

I did figure out the first part of the question. I just made my id an id*.

But I still don't know why I'm getting those others warnings, I bet it's something really simple.

jeremy.king
Dec 4, 2005, 10:12 PM
Nevermind.

caveman_uk
Dec 5, 2005, 02:44 AM
- (id)initWithObjects:(id *)objects count:(unsigned)count

Although it's possible to do stuff yourself like the above in objective-C it's much more common to send a group of objects in an array or a dictionary (depending upon your needs). Something like

[myObject myMethodWithArray:[NSArray arrayWithObjects: one, two, three, nil]];

Although my example is obviously more verbose and slower when you only ever use immutable objects I think the method is much more flexible if it can take any old array not just a list of objects. You also get to use the NSArray methods to access the elements rather than messing about with pointer arithmetic.