我在我的应用程序中使用了一个帮助器类来访问数据库并返回一个包含5个对象的数组,然后我将其分配给视图控制器中的属性。
在我的视图控制器中,我这样称呼它:
- (void)viewDidLoad
{
[super viewDidLoad];
DatabaseHelper *helper = [[DatabaseHelper alloc] init];
self.trailersArray = [helper retrieveTrailers];
// Set trailer for first round
self.trailer = [self.trailersArray objectAtIndex:0];
// Prepare audio player
[self prepareToPlay];
// Swoop film screen in
[self swoopFilmScreenInAndAddPlayButton];
// Fade title in
[self fadeInTitleScreen];
// Initialise button array and set buttons to hidden
self.buttonOutlets = [NSArray arrayWithObjects:self.button1, self.button2, self.button3, self.button4, nil];
[self hideButtons];
// Initialise rounds
self.round = [NSNumber numberWithInt:-1];
// Initialise score which will also update graphics
[self initialiseScore:self.round];
self.scoreInteger = 0;
// Start first round
self.round = [NSNumber numberWithInt:0];
NSLog([self.trailersArray description]);
// User will trigger playing with Play IBAction
// User will trigger stop playing with Stop Playing IBaction
}
我的问题是,一旦viewDidLoad完成,辅助对象看起来就像它的对象一样消失了,而我的self.trailersArray最终指向什么。
我该如何解决这个问题?尝试过深度复制,并在属性上使用retain属性但不起作用。
我不能使用类方法,因为它会破坏我的帮助对象数据库方法,但我对类方法如何解决这个内存问题很感兴趣?
感谢您的帮助。
艾伦
编辑:根据要求,下面的retrieveTrailers代码:
-(NSArray *)retrieveTrailers
{
// Get list of mp3 files in bundle and put in array
NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *dirContents = [fm contentsOfDirectoryAtPath:bundleRoot error:nil];
NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.mp3'"];
NSArray *onlyMP3s = [dirContents filteredArrayUsingPredicate:fltr];
NSMutableArray *arrayOfTrailersWithMP3s = [[NSMutableArray alloc] init];
// Query database for objects where (unique id) = (mp3 file title)
for(NSString *string in onlyMP3s)
{
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Trailer"];
NSString *stringWithNoFileExtension = [string stringByReplacingOccurrencesOfString:@".mp3" withString:@""];
request.predicate = [NSPredicate predicateWithFormat:@"unique = %@", stringWithNoFileExtension];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES];
request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *array = [[self managedObjectContext] executeFetchRequest:request error:nil];
Trailer *trailer = [array lastObject];
NSLog([NSString stringWithFormat:@"Trailer Available:%@", trailer.title]);
if(trailer)
{
[arrayOfTrailersWithMP3s addObject:trailer];
}
}
// Choose five of the trailers at random and return
NSMutableArray *fiveRandomSelectedTrailers = [[NSMutableArray alloc] init];
int numberOfAvailableTrailers = [arrayOfTrailersWithMP3s count];
for(int i=0;i<5;i++)
{
int rand = (arc4random() % (numberOfAvailableTrailers));
Trailer *trailer = [arrayOfTrailersWithMP3s objectAtIndex:rand];
NSLog([NSString stringWithFormat:@"Trailer Chosen:%@", trailer.title]);
[fiveRandomSelectedTrailers addObject:trailer];
[arrayOfTrailersWithMP3s removeObject:trailer];
numberOfAvailableTrailers --;
}
return fiveRandomSelectedTrailers;
}
答案 0 :(得分:1)
如果您真的是viewDidLoad
代码,那么您正在创建的辅助类的本地对象在函数完成后会超出范围。
如果你有一个helper类的保留属性,你不需要声明:尝试这样做
在你的.h文件中有这样一行:
@property(retain, nonatomic) DatabaseHelper *helper;
在您的.m文件中,请确保您拥有:
@synthesize helper;
在viewDidLoad
:
self.helper = [[DatabaseHelper alloc] init];
self.trailersArray = [self.helper retrieveTrailers];
这样,您将创建辅助类的对象并将其分配给属性,而不是创建局部变量。而且,正如您所看到的,当您想要发送消息时,可以使用助手类的属性对象。
我假设您使用的是MRC而不是ARC。