Objective-C有個很腦殘的問題
描述如下:
ASIFormRequest類是ASIHTTPRequest類的子類。在函數中,對另一地方返回的ASIFormRequest指標進行retain,出了這個函數,retain的指標可以訪問,但是所有從父類繼承的屬性、方法,都out of scope,一使用就程式崩潰。
解決辦法很簡單,retain時不要retain子類指標,retain父類的指標既可。
-(ASIHTTPRequest *) _getSinaRequest{
ASIHTTPRequest *request = [sinaApi uploadWithImage:selectedPhoto status:text.text];
request.delegate = self;
request.showAccurateProgress = YES;
request.uploadProgressDelegate = sinaPostProgress;
[request retain];
return request;
}
- (void)uploadPhoto
{
if ([sinaApi isAuthorized]) {
sinaPosted = NO;
sinaPostRequest = [self _getSinaRequest];
[sinaPostRequest startAsynchronous];
}
else {
sinaPosted = YES;
}
//... ...
}
- (void)cancel: (id)sender{
if(sinaPostRequest){
if(![sinaPostRequest isFinished]){ // 如果retain的是ASIFormRequest指標,這裡馬上死掉
[sinaPostRequest clearDelegatesAndCancel];
}
[sinaPostRequest release];
}
sinaPostRequest = nil;
[progressWindow hideImmediately];
}