Override Methods in the Objective-C Class

Why can't I do this, and how can I perform the same behavior in Objective-C?

@interface Test
{

}

- (void)test:(Foo *)fooBar;
- (void)test:(Bar *)fooBar;

@end

Thanks in advance!

+3
source share
3 answers

This is called overloading, not overriding. Objective-C methods do not support type overloading, only by method and parameter name (and "overload" is not really a good term for what happens anyway).

+5
source

The convention is to have options for the method name in accordance with the accepted parameters:

- (void)testWithFoo:(Foo *)foo;
- (void)testWithBar:(Bar *)bar;
+5
source

, . , Obj-C " " , , ; :

id objectOfSomeType = [foo methodReturningId]; //It not clear what class this is
[Test test:objectOfSomeType]; //Which method is called? I dunno! It confusing.

, , :

- (void)test:(id)fooBar
{
    if ([fooBar isKindOfClass:[Foo class]])
    {
        //Stuff
    }
    else if ([fooBar isKindOfClass:[Bar class]])
    {
        //You get the point
    }
}

, , :

- (void)testWithFoo:(Foo *)foo;
- (void)testWithBar:(Bar *)bar;
+4
source

Source: https://habr.com/ru/post/1739681/


All Articles