作为免责声明,我想说明我对Objective-C和Cocoa相当新。目前我正在尝试编写一个可以将XML数据发送到特定端点的基本应用程序。为此,我创建了一个ServiceRouter类,它使用NSURLConnection将XML数据发布到特定的URL。
ServiceRouter类旨在作为包含特定于Web服务的XML查询的子类的基础。在下面的示例中,我将ServiceRouter子类化为创建ServiceImplementation类。
当生成和发布XML时,我创建了一个ServiceImplementation类的实例,如下所示:
[[ServiceImplementation alloc] createServiceSpecificXML];
这一切似乎都很好。问题是Leaks报告了一些问题。由于缺乏经验,我不确定从哪里开始。在大多数情况下,NSURLConnection代码取自Apple的文档。
遵循基本的内存管理规则,我想我必须在某个时候发布我的ServiceImplementation实例。令我困惑的是,考虑到NSURLConnection的异步特性,应如何处理这个问题。这是自动释放的候选人吗?
我希望有更多Objective-C / Cocoa经验的人可以看一看并告诉我,我是否朝着正确的方向前进。
这是ServiceRouter类:
@interface ServiceRouter : NSObject {
NSMutableData *receivedData;
}
-(void)postXMLToURL:(NSString *)url xml:(NSString *)xmlData;
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
-(void)connectionDidFinishingLoading:(NSURLConnection *)connection;
@end
@implementation ServiceRouter
- (void)postXMLToURL:(NSString *)url xml:(NSString *)xmlData
{
NSLog(@"Posting XML to URL: %@", url);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/xml" forHTTPHeaderField:@"Content-type"];
[request setValue:@"application/xml" forHTTPHeaderField:@"Accept"];
[request setValue:[NSString stringWithFormat:@"%d", [xmlData length]] forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:[xmlData dataUsingEncoding: NSUTF8StringEncoding]];
NSURLConnection *connection = [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
if(connection) {
NSLog(@"Connection created");
receivedData = [[NSMutableData data] retain];
} else {
NSLog(@"Issue with connection!");
}
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
if([response respondsToSelector:@selector(statusCode)])
{
int statusCode = [((NSHTTPURLResponse *)response) statusCode];
NSLog(@"HTTP Response code: %i", statusCode);
}
NSLog(@"Received response");
[receivedData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"Received data: %@", data);
[receivedData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[connection release];
[receivedData release];
NSLog(@"Connection failed! Error - %@", [error localizedDescription]);
}
-(void)connectionDidFinishingLoading:(NSURLConnection *)connection
{
NSLog(@"Succeeded! Received %d bytes of data", [receivedData length]);
[connection release];
[receivedData release];
}
@end
这是我的ServiceImplementation类:
@interface ServiceImplementation : ServiceRouter {
}
-(void) createServiceSpecificXML;
@end
@implementation ServiceImplementation
-(void) createServiceSpecificXML
{
NSString *xmlData = @"<example><ignore/></example>";
[super postXMLToURL:@"http://site.com/endpoint.xml" xml:xmlData];
}
@end
答案 0 :(得分:1)
您永远不会初始化实例。仅仅分配是不够的。您必须致电init
或其他初始值设定项 - 最好与alloc
在同一行。
从[[NSMutableData data] retain]
的(工作但奇怪的)构造中,我猜你还没有读过很多Apple的基本引物。我建议至少The Objective-C Programming Language和memory management guide。两者都不长,在这两者之间,我认为你会清除很多不确定因素。