123hal321 123hal321 - 2 months ago 9
Objective-C Question

Can I overload an operator in Objective-C?

Is it possible to override operator use in Objective-C?

For example

myClassInstance + myClassInstance


calls a custom function to add the two.

Answer

Operator overloading is not a feature of Objective-C. If two instances of your classes can be added together, provide a method and allow them to be added using that method:

Thing *result = [thingOne thingByAddingThing:thingTwo];

Or, if your class is mutable:

[thingOne addThing:thingTwo];
Comments