這是殺死我!iPhone .. NSString被釋放&dealloc - 但我不知道在哪裏或爲什麼
我有一個看法。在.h文件我這樣做:
@interface SearchLogs : UIViewController <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, UIActionSheetDelegate, UIPickerViewDelegate> {
NSString *startDate;
NSDateFormatter *thisFormatter;
}
@property (nonatomic, retain) NSString *startDate;
@property (nonatomic, retain) NSDateFormatter *thisFormatter;
@end
有在@interface和@properties其他的東西......但是這是我做的startDate
。
我這樣做:
@implementation SearchLogs
@synthesize startDate;
@synthesize thisFormatter;
- (void)viewDidLoad {
NSLog(@"viewDidLoad\n");
[super viewDidLoad];
thisFormatter = [[NSDateFormatter alloc] init];
[thisFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *today = [[NSDate alloc] init];
NSLog(@"startDate refcount: '%i'\n",[startDate retainCount]);
startDate = [thisFormatter stringFromDate:today];
NSLog(@"startDate refcount: '%i'\n",[startDate retainCount]);
[today release];
}
- (void)viewWillAppear:(BOOL)animated {
NSLog(@"viewWillAppear\n");
[super viewWillAppear:animated];
NSLog(@"startDate refcount: '%i'\n",[startDate retainCount]);
}
- (void)viewDidAppear:(BOOL)animated {
NSLog(@"viewDidAppear\n");
[super viewDidAppear:animated];
NSLog(@"startDate refcount: '%i'\n",[startDate retainCount]);
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(@"numberOfSectionsInTableView\n");
// Return the number of sections.
NSLog(@"startDate refcount: '%i'\n",[startDate retainCount]);
return 6;
}
- (void)dealloc {
[startDate release];
[thisFormatter release];
}
這裏是我的問題:在numberOfSectionsInTableView
這裏我的應用程序崩潰是日誌:
2010-06-20 17:35:22.363 cConnect[10529:207] viewDidLoad
2010-06-20 17:35:22.376 cConnect[10529:207] startDate refcount: '0'
2010-06-20 17:35:22.378 cConnect[10529:207] startDate refcount: '1'
2010-06-20 17:35:22.378 cConnect[10529:207] viewWillAppear
2010-06-20 17:35:22.379 cConnect[10529:207] startDate refcount: '1'
2010-06-20 17:35:22.379 cConnect[10529:207] viewDidAppear
2010-06-20 17:35:22.380 cConnect[10529:207] startDate refcount: '1'
2010-06-20 17:35:22.381 cConnect[10529:207] numberOfSectionsInTableView
2010-06-20 17:35:22.381 cConnect[10529:207] *** -[CFString retainCount]: message sent to deallocated instance 0x5da5730
我的主要問題是爲什麼?我的代碼從未明確發佈過startDate
。有沒有我正在做的事情導致它不知道它被釋放?
TIA
稍向編輯:
我試圖取代:
startDate = [thisFormatter stringFromDate:today];
有:
startDate = [[thisFormatter stringFromDate:today] retain];
,它不再崩潰了!我認爲NSDateFormatter卡住,直到變量不再需要它... :(我誤解便利方法嗎?
我(因爲你可以看到)終於嘗試的(保留)的變量。我沒有考慮設置self.VARNAME ..浪費了一小時,試圖找出我做錯了什麼。我需要咖啡因!謝謝! – Jann 2010-06-21 01:07:45