私はobjective-cにかなり慣れていませんが、そのほとんどは明らかですが、メモリ管理に関しては少し足りません。現在、私のアプリケーションが行うのは、NSURLConnection中にメソッド-(void)connectionDidFinishLoading:(NSURLConnection *)connection
が呼び出されたときです。メソッドを入力してデータを解析し、それを配列に入れて、その配列を返します。ただし、カスタムメソッド(メソッド1、添付のコードを参照)内でメモリから配列を解放しないため、これが最善の方法であるかどうかはわかりません。
以下は、私が何をしているのかをよりよく示すための小さなスクリプトです
.hファイル
#import <UIKit/UIKit.h>
@interface memoryRetainTestViewController : UIViewController {
NSArray *mainArray;
}
@property (nonatomic, retain) NSArray *mainArray;
@end
.mファイル
#import "memoryRetainTestViewController.h"
@implementation memoryRetainTestViewController
@synthesize mainArray;
// this would be the parsing method
-(NSArray*)method1
{
// ???: by not release this, is that bad. Or does it get released with mainArray
NSArray *newArray = [[NSArray alloc] init];
newArray = [NSArray arrayWithObjects:@"apple",@"orange", @"grapes", "peach", nil];
return newArray;
}
// this method is actually
// -(void)connectionDidFinishLoading:(NSURLConnection *)connection
-(void)method2
{
mainArray = [self method1];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
mainArray = nil;
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[mainArray release];
[super dealloc];
}
@end