Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

BadWolf13

macrumors 6502
Original poster
Dec 17, 2009
271
0
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]
 
"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**.
 
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?
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.