第一個是標準分配,而第二個意味着返回autorelease
d對象的類方法。
假設您在控制器(或viewController)內部有此代碼,則在方法結束時,request
對象可能會被釋放。 所以你需要在你的控制器中保留對這個對象的引用,這樣它纔不會被釋放。
所以,你的接口定義:
@interface MyController : UIViewController {
ASIHTTPRequest *request;
// ...
}
@property (nonatomic, retain) ASIHTTPRequest *request;
@end
然後實施
@implementation MyController
@sinthetyze request;
- (void) aMethodThatInstantiatesYourRequest { // using self.request means that we use the accessory method, which involves a retain call
self.request = [[ASIHTTPRequest alloc] initWithURL:myURL];
// ...
[request release];
// or using the autoreleased object
self.request = [ASIHTTPRequest requestWithURL:myURL];
}
- (void) dealloc { // don't forget to release the request object
[request release];
[super release];
}
順便說一句,如果你有一個以上的要求,這將是更好地使用網絡隊列,因此您保留對隊列的引用,而不是針對各種單個請求。
See this gist,它使用ASINetworkQueue
。
發佈你的完整代碼以獲得更好的答案。 –