PDA

View Full Version : Warning for using an id pointer argument.




BadWolf13
Sep 4, 2010, 01:31 AM
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.


-(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.

[self firstValueWithLabel:multiValue:kABEmailWorkLabel:&email]



vocaro
Sep 4, 2010, 02:18 AM
Should be (id*), not (id**).

gnasher729
Sep 4, 2010, 02:39 AM
"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**.

BadWolf13
Sep 4, 2010, 01:22 PM
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?

gnasher729
Sep 4, 2010, 01:39 PM
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?

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.