1.14. Definiowanie dwóch lub wielu metod o tej samej nazwie

Problem

W jednym obiekcie chcesz zaimplementować dwie metody lub kilka metod o tej samej nazwie. W programowaniu zorientowanym obiektowo działanie takie nosi nazwę przeciążenia metody. Jednak w języku Objective-C przeciążanie metody nie istnieje w takiej samej postaci, która jest znana z innych języków programowania, np. C++.

Rozwiązanie

Użyj takiej samej nazwy dla metod, ale w każdej z nich utwórz inną liczbę parametrów bądź nadaj im inne nazwy.

- (void)drawRectangle { [self drawRectangleInRect:CGRectMake(0.0f, 0.0f, 4.0f, 4.0f)]; } - (void)drawRectangleInRect:(CGRect)paramInRect { [self drawRectangleInRect:paramInRect withColor:[UIColor blueColor]]; } - (void)drawRectangleInRect:(CGRect)paramInRect ...

Get iOS 5. Programowanie. Receptury now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.