我在ObjC中編寫的前幾個程序運行良好,但混亂不堪,所以這次我想做正確的事,並且使用MVC。所有的部分工作,並已經過測試,事情進展順利,直到我試圖將模型中的NSMutableArray通過VC複製到視圖。使用完全相同的格式和代碼,並在程序的另一個方面工作正常,但如果我不保留數組,則此特定視圖使用drawRect並中斷。當我這樣做會導致泄漏。爲了隔離問題並創建解決方法,我最終直接從pList加載數組。它看起來像這樣:NSArray不會在我的MVC視圖中初始化
@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];」它崩潰了。任何幫助表示讚賞。
也許你想強壯而不是保留? – 2012-12-06 02:56:37