English 中文(简体)
How to release attribute which is protocol?
原标题:

I have working for iPhone development for a while. First time, I so surprised with memory-management in objective-c :). but now I got it a little bit.

The question is, sometime, I use protocol as an attribute of a class, because I think its definition very similar to interface in C# or Java. like below.

@protocol Shield
   ...
@end

// Interface
@interface Dragon {
    id<Shield> shield
    NSString * name;
}

@property (nonatomic,retain) id<Shield> shield;
@property (nonatomic,retain) NSString * name;

@end

but I alway release any attribute object in dealloc() method. like below.

-(void)dealloc {
   [name release];
   [shield release];  // <--- Totally impossible. xcode said  -release not found in protocol 
   [super dealloc];
 }

As you see, I couldn t release the protocol. So would this cause me future memory issue? Do you have another way to handle this solution to advice me?

最佳回答

You need to define your protocol as adhering to the NSObject protocol, like this:

@protocol Shield <NSObject>
   ...
@end

It s simple when you know how! ;-)

Edit: Also, you re correct - protocols in Objective-C are equivalent to interfaces in Java and C#.

Another edit: It might strike you as odd having to do this, but Objective-C allows for multiple root objects, so you can t actually guarantee that every object will descend from NSObject. Since release is an NSObject method, you have to define your protocol as also adhering to the <NSObject> protocol before you can be sure it ll be able to respond to the release method.

问题回答

1-the proper thing to do instead of [shield release] is setting

self.shield = nil;

2-Also change

@property (nonatomic,retain) id<Shield> shield; 

to

@property (nonatomic,assign) id<Shield> shield;

Then you are fine.

edit:

The reason that you avoid retaining delegates is that you need to avoid a retain loop:

A creates B A sets itself as B s delegate … A is released by its owner

If B had retained A, A wouldn t be released, as B owns A, thus A s dealloc would never get called, causing both A and B to leak.

You shouldn t worry about A going away b/c it owns B and thus gets rid of it in dealloc.

Why are Objective-C delegates usually given the property assign instead of retain?

please see uitableview class reference for an example protocol declaration:

@property(nonatomic, assign) id < UITableViewDelegate> delegate





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签