Objective-C
method overloading
programming
software development
coding fundamentals

Method overloading in Objective-C?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Objective-C does not support method overloading — you cannot define two methods with the same selector name but different parameter types. This is because Objective-C method dispatch is based on the selector (method name), not on the parameter types. In languages like C++ or Java, void draw(int x) and void draw(String s) can coexist. In Objective-C, the selector is the method name including all parameter label keywords, and two methods with identical selectors cannot exist in the same class. The workaround is to use different method names that describe their parameters.

How Method Dispatch Works in Objective-C

In Objective-C, methods are dispatched using selectors — a selector is the full method name including colons and keyword labels. The runtime looks up methods solely by selector, not by parameter types.

objc
1// These are TWO DIFFERENT selectors:
2- (void)drawWithRect:(CGRect)rect;         // Selector: drawWithRect:
3- (void)drawWithPath:(UIBezierPath *)path; // Selector: drawWithPath:
4
5// These would be the SAME selector (ILLEGAL — compile error):
6- (void)draw:(int)number;     // Selector: draw:
7- (void)draw:(NSString *)text; // Selector: draw: — CONFLICT

The compiler will reject the second draw: method with a "duplicate declaration" error because both have the identical selector draw:.

Using Distinct Selectors Instead of Overloading

The Objective-C convention is to encode the parameter type or purpose into the method name itself.

objc
1@interface ShapeRenderer : NSObject
2
3// Instead of overloading "draw:", use descriptive names
4- (void)drawWithRect:(CGRect)rect;
5- (void)drawWithCircle:(CGFloat)radius center:(CGPoint)center;
6- (void)drawWithPath:(UIBezierPath *)path;
7- (void)drawWithImage:(UIImage *)image atPoint:(CGPoint)point;
8
9@end
objc
1@implementation ShapeRenderer
2
3- (void)drawWithRect:(CGRect)rect {
4    NSLog(@"Drawing rectangle: %@", NSStringFromCGRect(rect));
5}
6
7- (void)drawWithCircle:(CGFloat)radius center:(CGPoint)center {
8    NSLog(@"Drawing circle with radius %.1f at (%.1f, %.1f)", radius, center.x, center.y);
9}
10
11- (void)drawWithPath:(UIBezierPath *)path {
12    [path stroke];
13}
14
15- (void)drawWithImage:(UIImage *)image atPoint:(CGPoint)point {
16    [image drawAtPoint:point];
17}
18
19@end

Using id Type for Generic Parameters

If you want a single method that accepts different types, use id (any object) and check the type at runtime.

objc
1- (void)display:(id)object {
2    if ([object isKindOfClass:[NSString class]]) {
3        NSLog(@"String: %@", object);
4    } else if ([object isKindOfClass:[NSNumber class]]) {
5        NSLog(@"Number: %@", object);
6    } else if ([object isKindOfClass:[UIImage class]]) {
7        NSLog(@"Image: %@", [object description]);
8    } else {
9        NSLog(@"Unknown type: %@", [object class]);
10    }
11}

This achieves runtime polymorphism but loses compile-time type safety.

Variadic Methods

Objective-C supports variadic methods (variable number of arguments) using C-style va_list.

objc
1- (void)logMessages:(NSString *)firstArg, ... {
2    va_list args;
3    va_start(args, firstArg);
4
5    NSString *arg = firstArg;
6    while (arg != nil) {
7        NSLog(@"%@", arg);
8        arg = va_arg(args, NSString *);
9    }
10
11    va_end(args);
12}
13
14// Usage — must be nil-terminated
15[self logMessages:@"Hello", @"World", @"Goodbye", nil];

Comparison with Swift

Swift (which interoperates with Objective-C) supports method overloading by parameter type.

swift
1class Renderer {
2    func draw(_ rect: CGRect) { /* ... */ }
3    func draw(_ path: UIBezierPath) { /* ... */ }
4    func draw(_ number: Int) { /* ... */ }
5    // All three coexist — Swift dispatches by parameter type
6}

When Swift methods are exposed to Objective-C via @objc, they must have distinct selectors. Swift generates unique selectors automatically or you can specify them:

swift
1@objc(drawWithRect:)
2func draw(_ rect: CGRect) { /* ... */ }
3
4@objc(drawWithPath:)
5func draw(_ path: UIBezierPath) { /* ... */ }

Common Pitfalls

  • Expecting C++/Java-style overloading to work: Defining two methods with the same selector but different parameter types causes a compile error. Always encode the parameter type or purpose into the method name (e.g., initWithString:, initWithInt:, initWithData:).
  • Using id without type checking: Accepting id parameters provides flexibility but crashes at runtime if you call type-specific methods on the wrong type. Always check with isKindOfClass: or respondsToSelector: before using type-specific APIs.
  • Forgetting nil terminator on variadic methods: C-style variadic methods in Objective-C have no way to know how many arguments were passed. The convention is nil-termination. Omitting the trailing nil causes undefined behavior as va_arg reads garbage memory.
  • Confusing selectors with method signatures: The selector initWithName:age: is identified purely by the string "initWithName:age:". The types of the parameters (NSString, int, id) are part of the method signature but not the selector. Two methods differing only in parameter types share a selector and cannot coexist.
  • Not leveraging Objective-C naming conventions: The Cocoa naming convention encodes type information into method names naturally: stringWithFormat:, numberWithInt:, arrayWithObject:. Following this convention eliminates the need for overloading and makes the API self-documenting.

Summary

  • Objective-C does not support method overloading — dispatch is based on selector name, not parameter types
  • Use distinct method names that describe their parameters: drawWithRect:, drawWithPath:, drawWithImage:atPoint:
  • Use id type with runtime type checking (isKindOfClass:) for generic methods
  • Use variadic methods with nil-termination for variable argument counts
  • Swift supports overloading but must generate unique Objective-C selectors when bridged

Course illustration
Course illustration

All Rights Reserved.