Can I overload an operator in Objective-C?

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];

No, you can't do this in Objective-C.


You can do this now in Swift, a successor to objC. And since Objective-C and Swift are made to work together This could be interesting for you.


You may want to support subscripting for your object. Subscripting is not operator overloading, but it can be handy for a collection object. NSArray and NSDictionary both support subscripting. For example:

NSMutableArray *a = [NSMutableArray new]; a[0] = @"Hello";

The way to support index subscripting is to implement the following:

-(id)objectAtIndexedSubscript:(NSUInteger)idx; -(void)setObject:(id)newObject atIndexedSubscript:(NSUInteger)idx];