我遇到了一些Objective-C的问题,并希望得到一些指示。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;
}
我得到的问题是当我想使用这个类 - 显然是由于我对记忆管理的误解。
在我的视图控制器我声明如下:
(在@interface):
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类的实例变量?创建对象的正确方法是添加到类中,以便在我完成之前不会释放它们?
非常感谢
感谢您的帮助 - 我一直在阅读内存管理规则,但仍然每时每刻都被绊倒(有点太习惯于新的和免费的我认为)。我仍然有一个数组中的范围内的问题,但是,'mapfile = [[MapFile alloc] initWithName:nameOfMap]; [mapfile retain]; //添加到数组 [fullGroupOfMaps addMapFile:mapfile];'添加它们,但它们超出范围。我认为addObject会执行保留? – davbryn 2010-05-07 09:39:35
我的不好,在C++和Obj-C之间不会让生活变得轻松。我一直在为我的类创建@properties,但没有使用self.variable_name来获取/设置,因此没有发生保留:( – davbryn 2010-05-07 10:20:48