I have a function that I want to operate on two different custom objects. My first thought was to accept the argument as an (id) and operate on the id object. I can\'t quit
While you can send a message to any object (id) - property accessors require that the compiler be aware of the type you are dealing with - this is because property accessors are syntactic sugar around calling specific getter and setter methods.
You have a few of ways of working around this:
Instead of accessing the count property, call the corresponding [getCount] methods.
If the different classes have different versions of this method, you can use a runtime type check:
Provide a base class for both types so that you can pass in something more specific than (id).
Example of a dynamic type check:
if( [object isKindOfClass:[Apple Class] )
// call one overload of getCount
else if( [object isKindOfClass:[Orange Class] )
// call another overload of getCount
Personally, I favor strong typing in my code because it makes it easier to understand the intent. It also allows the IDE to support your coding effort with intellisense, static analysis, and refactoring features. So, in your case, I would use either #3 or #4 as an approach - depending on whether inheritance is really appropriate for the problem.
The fact that you are trying to access 'addFruit.count' is the problem. The dot syntax is only for properties declared with @property (or for structs). If you change it to
[addFruit count]
and add
-(NSDecimalNumber*)count
{
return [[count retain] autorelease];
}
to each class, then it would work. However, you will notice you'll get a warning saying 'id' may not respond to the 'count' message, and unless you can be absolutely sure the items sent to this method implement a 'count' method, this is a problematic approach.
I agree with pgb's approach. You should define a protocol, and declare both classes to implement that protocol. This eliminates the problem of not knowing whether the object will respond to 'count' or not, as you now have a 'contract' of sorts.
If you want to keep the dot syntax with a property, you can declare it in the protocol:
@protocol FruitProtocol
@property(readonly) NSDecimalNumber * count;
- (NSDecimalNumber *)count
@end
and then, your function would be:
-(NSDecimalNumber*)addCount:(id<FruitProtocol>)addObject{
return [count decimalNumberByAdding:addObject.count];
}
You're sending the message to count, what is count? id is a pointer to any type of object. If you expect the object to have a count property, then you should only be able to pass in an Array (or some other type restriction).
-(NSDecimalNumber*)addCount:(NSArray*) Object{
return [count decimalNumberByAdding: [Object count]];
}
You should try not to access instance variables from another class.
In Objective-C it's enough that the two objects respond to the same selector (say count
), however that would give you a compiler warning.
There are two ways you can get rid of this warning: either by subclassing from a common Fruit
class or by having your two classes conform to a protocol. I'd go with the protocol:
@protocol FruitProtocol
- (NSDecimalNumber *)count;
@end
@interface Orange : NSObject<FruitProtocol>
@end
@interface Apple : NSObject<FruitProtocol>
@end
Then your method can look like this:
-(NSDecimalNumber*)addCount:(id<FruitProtocol>)addFruit {
return [count decimalNumberByAdding:[addFruit count]];
}
Here you are saying that your addCount
expects any object that conforms to the FruitProtocol
protocol, and hence can respond to the count
selector, so the compiler will accept it.
As I understand it, id
does not have any methods or variables associated with it because it is a generic pointer that does not refer to any specific class. This page has some good info on ids if you scroll down a bit.
anObject this will not have a count
variable, which is why your first attempt won't work. Creating a base class and using that as a parameter to the method seems like the best idea to me.