NSArray 不会在我的 MVC 视图中初始化
我在 ObjC 中编写的前几个程序可以工作,但是一团糟,所以这次我想正确地执行并使用 MVC。所有的部分都可以工作并且已经过测试,一切进展顺利,直到我尝试通过 VC 将 NSMutableArray 从模型复制到视图。使用了完全相同的格式和代码,并且在程序的另一个方面工作得很好,但是这个特定的视图使用了drawRect,并且如果我不保留数组就会中断。当我这样做时,会导致泄漏。为了隔离问题并创建解决方法,我最终直接从 pList 加载数组。它看起来像这样:
@interface HWView : UIView <UIGestureRecognizerDelegate>
{
NSMutableArray *drawStates;
}
@property (nonatomic, retain) NSMutableArray *drawStates;
在 .m 中
@implementation HWView
@synthesize drawStates;
- (void)awakeFromNib
{
[self HWVReset];
}
-(void)HWVReset
{
NSLog(@"HWVReset:");
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [documentsDirectorystringByAppendingPathComponent: @"PLIST_drawState.plist"];
self.drawStates = [[NSMutableArray arrayWithContentsOfFile:plistPath]retain];
NSLog(@"drawStates:%@",self.drawStates);
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
//draw code
}
- (void)dealloc
{
[self.drawStates release];
[super dealloc];
}
所以它运行但它泄漏。我从以下位置删除保留:“self.drawStates = [[NSMutableArray arrayWithContentsOfFile:plistPath]retain];”它崩溃了。任何帮助表示赞赏。
The first few programs I wrote in ObjC worked but were a cluttered mess so this time I wanted to do it right and employ MVC. All of the pieces work and have been tested and things were going great until I tried to copy an NSMutableArray from the model through the VC to the view. The exact same format and code was used and works fine in another aspect of the program but this particular view uses drawRect and breaks if I don't retain the array. When I do it causes a leak. To isolate the problem and create a workaround I ended up loading the array directly from the pList. It looks like this:
@interface HWView : UIView <UIGestureRecognizerDelegate>
{
NSMutableArray *drawStates;
}
@property (nonatomic, retain) NSMutableArray *drawStates;
in the .m
@implementation HWView
@synthesize drawStates;
- (void)awakeFromNib
{
[self HWVReset];
}
-(void)HWVReset
{
NSLog(@"HWVReset:");
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [documentsDirectorystringByAppendingPathComponent: @"PLIST_drawState.plist"];
self.drawStates = [[NSMutableArray arrayWithContentsOfFile:plistPath]retain];
NSLog(@"drawStates:%@",self.drawStates);
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
//draw code
}
- (void)dealloc
{
[self.drawStates release];
[super dealloc];
}
So this runs but it leaks. I remove the retain from: "self.drawStates = [[NSMutableArray arrayWithContentsOfFile:plistPath]retain];" and it crashes. Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然:[NSArray array] 是一个自动释放的对象。所以你将调用一个已释放的对象。阅读 Apple 文档中有关 Autorelease 的内容。
Of course : [NSArray array] is an autoreleased object. So you'll be calling a deallocated object. Read about Autorelease in Apple's docs.