Warning for using an id pointer argument.

Discussion in 'Mac Programming' started by BadWolf13, Sep 3, 2010.

  1. macrumors 6502

    Joined:
    Dec 17, 2009
    #1
    Long and short is I wrote the following method which involves a pointer argument. Since the argument can be of different classes, I thought an id would be the best idea, but it gives me warnings for passing NSString and NSDictionary classes into the method. The exact warning is "Passing argument 3 of firstValueWithLabel:::' from incompatible pointer type."

    Now the program still runs the way I want and expected it to do, so I really don't understand the warning, but I want to be aware if there's something that could cause issues down the road with this program.

    Here's the code of the parts in question.

    Code:
    -(BOOL)firstValueWithLabel:(ABMultiValue *)multiValue:(NSString *)label:(id **)result{
    	int x;
    	
    	for (x=0; x<[multiValue count]; x++) {
    		if ([[multiValue labelAtIndex:x] isEqual:label]) {
    			*result = [multiValue valueAtIndex:x];
    			return true;
    		}
    	}
    	return false;
    }
    
    And here's one of the lines where I call the method.

    Code:
    [self firstValueWithLabel:multiValue:kABEmailWorkLabel:&email]
     
  2. macrumors regular

    Joined:
    Mar 5, 2004
    #2
    Should be (id*), not (id**).
     
  3. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #3
    "id" is the same as "pointer to any NSObject". Note that it is "id" and not "id *". So the address of an NSObject* is id*, not id**.
     
  4. thread starter macrumors 6502

    Joined:
    Dec 17, 2009
    #4
    That did it, thanks guys.

    One question just for understanding's sake. If I wanted to pass an argument of type NSObject into a method, the argument would be typed as;

    (id)anObject

    Is that correct?
     
  5. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #5
    Well, you can't pass an NSObject to anything, you can only pass an NSObject*. You can pass any NSObject* to a method or function that expects an id; no cast is necessary (the compiler lets you assign any pointer to an NSObject to an id variable, and any id to any NSObject* variable).

    "id" (without the *) in Objective-C plays roughly the same role as "void *" (with the *) in C.
     

Share This Page