我在使用一些 Objective-C 时遇到了一些问题,希望得到一些指点。
所以我有一个MapFileGroup
具有以下简单接口的类(还有其他成员变量,但它们并不重要):
@interface MapFileGroup : NSObject {
NSMutableArray *mapArray;
}
@property (nonatomic, retain) NSMutableArray *mapArray;
mapArray
在@synthesize
.m 文件中。
它有一个init方法:
-(MapFileGroup*) init
{
self = [super init];
if (self)
{
mapArray = [NSMutableArray arrayWithCapacity: 10];
}
return self;
}
它还有一个向数组添加自定义对象的方法:
-(BOOL) addMapFile:(MapFile*) mapfile
{
if (mapfile == nil) return NO;
mapArray addObject:mapfile];
return YES;
}
当我想使用这个类时遇到的问题 - 显然是由于我对内存管理的误解。
在我的视图控制器中,我声明如下:
(在@界面中):
MapFileGroup *fullGroupOfMaps;
使用@property@property (nonatomic, retain) MapFileGroup *fullGroupOfMaps;
然后在 .m 文件中,我有一个名为的函数loadMapData
,它执行以下操作:
MapFileGroup *mapContainer = [[MapFileGroup alloc] init];
// create a predicate that we can use to filter an array
// 对于所有以 .png 结尾的字符串(不区分大小写) NSPredicate *caseInsensitivePNGFiles = [NSPredicate predicateWithFormat:@"SELF endswith[c] '.png'"];
mapNames = [unfilteredArray filteredArrayUsingPredicate:caseInsensitivePNGFiles];
[mapNames retain];
NSEnumerator * enumerator = [mapNames objectEnumerator];
NSString * currentFileName;
NSString *nameOfMap;
MapFile *mapfile;
while(currentFileName = [enumerator nextObject]) {
nameOfMap = [currentFileName substringToIndex:[currentFileName length]-4]; //strip the extension
mapfile = [[MapFile alloc] initWithName:nameOfMap];
[mapfile retain];
// add to array
[fullGroupOfMaps addMapFile:mapfile];
}
这似乎工作正常(虽然我可以说我没有让内存管理正常工作,但我仍在学习 Objective-C);但是,我有一个(IBAction)
与后者交互的fullGroupOfMaps
。它在 中调用一个方法fullGroupOfMaps
,但是如果我在调试时从该行进入类,所有fullGroupOfMaps
的对象现在都超出了范围,我会崩溃。
因此,为冗长的问题和大量的代码道歉,但我想我的主要问题是:
我应该如何处理一个以 NSMutableArray 作为实例变量的类?创建要添加到类中的对象的正确方法是什么,以便在我完成它们之前它们不会被释放?
非常感谢