JordanMazurke
JordanMazurke

Reputation: 1123

Objective-C iOS Method invocation through protocols

I'm working on a design issue for my iOS app regarding protocols. Now I understand the usage, and when a protocol is required, and I also understand that they are analogous to interfaces in C# and Java.

Can you call methods through the protocol itself? For example, in C# I can do the following:

public interface IInterface
{
    void SomeMethod();
    void SomeOtherMethod();
}


public class AClass : IInterface
{
    public void SomeMethod()
    {
        //Do something
    }

    public void SomeOtherMethod()
    {
        //Do something
    }
}


public class Program
{
    public void Main()
    {
         IInterface i = new AClass();
         i.SomeMethod();
    }
}

Is this possible in Objective-C or am I trying to force a .NET style approach into iOS?

Upvotes: 0

Views: 352

Answers (1)

kovpas
kovpas

Reputation: 9593

@protocol Protocol
- (void)someFunction;
@end

@interface A <Protocol>
@end

@implementation A
- (void) someFunction
{
    ....
}
@end

In some other place

id<Protocol> a = [[A alloc] init];

[a someFunction];

[a release];

Upvotes: 4

Related Questions