iPhone .. NSString正在发布和dealloc - 但我不知道在哪里或为什么

Jan*_*ann 0 iphone sdk release nsstring retain

这就是杀了我!

我有一个观点.在.h文件中我这样做:

@interface SearchLogs : UIViewController <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, UIActionSheetDelegate, UIPickerViewDelegate> {

NSString *startDate;
NSDateFormatter *thisFormatter;
}


@property (nonatomic, retain) NSString *startDate;
@property (nonatomic, retain) NSDateFormatter *thisFormatter;
@end
Run Code Online (Sandbox Code Playgroud)

@interface和@properties中还有其他东西......但这就是我所做的startDate.

在.m文件中我这样做:

@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];

}
Run Code Online (Sandbox Code Playgroud)

这是我的问题:我的应用程序崩溃了 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
Run Code Online (Sandbox Code Playgroud)

我的主要问题是为什么? startDate永远不会在我的代码中明确发布.我有什么办法让它在不知情的情况下被释放吗?

TIA

轻微编辑:

我试过更换:

startDate = [thisFormatter stringFromDate:today];
Run Code Online (Sandbox Code Playgroud)

有:

startDate = [[thisFormatter stringFromDate:today] retain];
Run Code Online (Sandbox Code Playgroud)

它不再崩溃!我以为NSDateFormatter陷入困境,直到变量不再需要它...... :(我是否误解了便利方法?

Eik*_*iko 6

你的问题是

 startDate = [thisFormatter stringFromDate:today];
Run Code Online (Sandbox Code Playgroud)

这会给你一个自动释放的字符串.你必须保留它.要么使用

startDate = [thisFormatter stringFromDate:today];
[startDate retain];
Run Code Online (Sandbox Code Playgroud)

或者使用该属性并调用setter:

self.startDate = [thisFormatter stringFromDate:today];
Run Code Online (Sandbox Code Playgroud)