Compiler Warnings: id and *id, multiple declarations

Discussion in 'Mac Programming' started by Jordan72, Dec 3, 2005.

  1. macrumors member

    Joined:
    Nov 23, 2005
    #1
    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.

    Code:
    -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:

    Code:
    [[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.

    Code:
    [self possible:[self possible] * [[[sets objectAtIndex:i] set] length]];
    
    warning: multiple declarations for method `length'
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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).
     
  3. thread starter macrumors member

    Joined:
    Nov 23, 2005
    #3
    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:

    Code:
    - (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.
     
  4. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
  5. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #5
    - (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.
     

Share This Page