這是我的場景。我有一個類A.在它的實現中,我創建了B類型的對象,並將B的委託設置爲self(因此,在A類實現中的某個位置處有B.delegate = self
)。ARC問題 - 無法傳遞方法委託?
和類A有一個實例方法- (void)printThis;
現在B的實施中,當我嘗試做[delegate printThis];
,它給了我這個錯誤:
當然 「爲選擇printThis沒有已知的實例方法」這是我啓用ARC時。上面的委託模式用於在沒有ARC的情況下在iOS 4.x中正常工作。當我關閉ARC時,它仍然有效。 ARC與傳遞消息給代表有什麼關係?
骨架代碼:
A.H
@class B;
@interface A: blah blah
{
B objB;
}
-(void) printThis;
A.M
objB = [[B alloc] init];
objB.delegate = self;
- (void)printThis {
//doSomething
}
B.h
@interface B: blah blah
{
//id delegate; //used to be there, now I just property & synthesize
}
@property (nonatomic,weak) id delegate;
B.m
@synthesize delegate;
[delegate printThis]; //error with ARC ON, works with OFF
重要編輯:
你得注意這種情況的在這裏和那裏的方法。例如,我有一些其他的方法,如printThat等等,工作沒有錯誤。我對所發生的事情無能爲力!
只是好奇,如果你通過'委託'的getter而不是直接訪問伊娃,你仍然有ARC的問題嗎?無論哪種方式,協議是更好的設計。 –