开发者

Memory management for "id<ProtocolName> variableName" type properties

开发者 https://www.devze.com 2022-12-31 19:28 出处:网络
I\'m having a problem with properties of the following type: id<ProtocolName> variableName; .....

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 interf开发者_StackOverflow社区ace, or how do I release the memory reserved for the variable?

Thanks!


Make your protocol adopts NSObject.

@protocol ProtocolName <NSObject>
...


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.


You can always add NSObject to the protocol list.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号