views:

30

answers:

3

Hi,

I'm having a problem with properties of the following type:

id<ProtocolName> variableName;
.....
.....
@property (nonatomic, retain) id<ProtocolName> variableName;

I can access and use them just fine, but when I try to call

[variableName release];

I get compiler warnings:

'-release' not found in protocol(s)

Do I need to define a release method in the interface, or how do I release the memory reserved for the variable?

Thanks!

+3  A: 

release is in the NSObject protocol, so you can declare it as id<ProtocolName, NSObject> to work within the type system or cast it to a plain id to stop its class from being typechecked.

Chuck
+1  A: 

You can always add NSObject to the protocol list.

Paul Lynch
+3  A: 

Make your protocol adopts NSObject.

@protocol ProtocolName <NSObject>
...
KennyTM
This assumes that you have control over the protocol definition — but if you do, yes, this is a good solution.
Chuck
In this case I do have control over the protocol definition - added NSObject and it works perfectly. Thanks! :)
Malakim
Just to expand slightly on this. If a protocol `AProtocol` does not adopt `NSObject` and you receive an object of type `id<AProtocol>` you cannot assume that the object responds to `-retain` and `-release`. There is nothing in Objective-C that says an object *has* to inherit from `NSObject`. `NSObject` is not part of the Objective-C language, it is part of the Foundation framework.
JeremyP