NSArray 不会在我的 MVC 视图中初始化

发布于 2024-11-07 03:29:44 字数 1201 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那些过往 2024-11-14 03:29:44

当然:[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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文