我对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