使用ARC时声明委托

时间:2013-08-17 22:07:11

标签: iphone ios6 delegates automatic-ref-counting

在fastpdfkit中有像这样的委托声明

@interface BookmarkViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {

//Delegate to get the current page and tell to show a certain page. It can also be used to
//  get a list of bookmarks for the current document. 

NSObject<BookmarkViewControllerDelegate> *delegate; 
 }

@property (nonatomic, assign) NSObject<BookmarkViewControllerDelegate> *delegate;

@synthesize delegate;

因为我使用ARC所以委托的声明就像这样

@interface BookmarkViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {

 id __unsafe_unretained <BookmarkViewControllerDelegate> delegate;
 }

@property (unsafe_unretained) id <BookmarkViewControllerDelegate> delegate;

 @synthesize delegate;

当我正在调试

时,它是否正确?
 currentPage    NSUInteger  0
delegate    objc_object *   0x00000000

1 个答案:

答案 0 :(得分:5)

接下来是一种模式。委托被正确地宣布为弱(一个对象但没有所有权转移或保留计数增加)。

@protocol MyClassDelegate;

@interface MyClass : NSObject

@property(weak, nonatomic) id<MyClassDelegate>delegate;

@end

@protocol MyClassDelegate <NSObject>

- (void)myClass:(MyClass *)myClass didSomethingAtTime:(NSDate *)time;
- (CGSize)sizeofSomethingNeededByMyClass:(MyClass *)myClass;

// and so on

@end