I need to be able to create an interface like you create in C# to enforce a group of classes to implement certain methods. Is this possible in objective c?
Interfaces are called protocols in Objective C
If you go to add a class to your project you will have an option to create it as protocol and you will end up with something like:
@protocol MyProtocol
-(void) DoSomething;
@end
You can create a protocol. it would look something like this: in MyProtocol.h:
@protocol MyProtocol
-(void)myMethod;
-(void)myMethod2;
@end
in MyClass.h
#import "MyProtocol.h"
@interface MyClass : NSObject<MyProtocol>
@end
If you want to receive objects of a given protocol, you can do so like this:
id<MyProtocol> var;
or
NSObject<MyProtocol> *var;
more info here
You declare a "protocol" (Objective-C's interfaces) using
@protocol MyProtocol <BaseProtocol1,...,BaseProtocolN>
//methods and properties
@end
where <BaseProtocol>
is optional and indicates that MyProtocol
"inherits" BaseProtocol
's interface. The NSObject
protocol is useful in this context because it allows you to use
@protocol MyProtocol <NSObject>
//...
@end
to indicate (when appropriate) that conforming instances of MyProtocol
have the standard NSObject
methods as well (e.g. -retain/-release
, etc.).
You then declare that a class "conforms" to a protocol:
@interface MyClass : NSObject <MyProtocol,...,OtherProtocols>
{}
@end
And you can test whether an instance conforms to a protocol:
id myInstance = ...; //some object instance
if([myInstance conformsToProtocol:@protocol(MyProtocol)]) {
// myInstance conforms to MyProtocol
}
You can further silence compiler warnings by declaring that a variable holds instances that conform to a protocol (note that Objective-C's dynamic nature prevents the compiler from verifying that contract and you can still get runtime errors by assigning a non-conforming instance to the variable):
id<MyProtocol> o;
In this case, the compiler will complain if you send [o retain]
without MyProtocol
conforming to the NSObject
protocol. You can silence these warnings by declaring MyProtocol
as conforming to NSObject
as described above or by delcaring o
as
NSObject<MyProtocol> o;
Since NSObject
is not the only root object in Cocoa (i.e. NSProxy
does not inherit from NSObject
), it's not necessarily true that all instances conforming to MyProtocol
also conform to NSObject
. If you know that they do, you can declare MyProtocol
as conforming to NSObject
.
Hello,
In the Objective-C world, interfaces are called "Protocols"
According to Apple,
Protocols declare methods that can be implemented by any class. Protocols are useful in at least three situations:
To declare methods that others are expected to implement
To declare the interface to an object while concealing its class
To capture similarities among classes that are not hierarchically related
So, to declare a protocol you can:
@protocol SomeClassProtocol
- (void) do:(int) number something:(id) sender;
- (void) somethingElse;
@end
To implement a protocol , you do:
@interface MyClass : NSObject <Protocol1, Protocol2, ..., ProtocolN>
@end
Check out this link for the official documentation.