我对这段代码有一个问题,我试图将一些数据保存到plist供以后使用,如果用户重新打开视图,它应该为我的视图中的每个段控件选择当前段。
然而,当我尝试保存信息时,它会在线路上崩溃
[array addObject:gender];
请帮助:)它给出了EXC_BAD_ACCESS
-(IBAction)genderSelection:(id)sender
{
if([sender selectedSegmentIndex] == kSwitchesSegmentIndex)
{
gender = @"Male";
//NSLog(gender);
}
else {
gender = @"Female";
//NSLog(gender);
}
}
-(IBAction)contactTypeSelection:(id)sender
{
if([sender selectedSegmentIndex] == kSwitchesSegmentIndex)
{
contactType = @"a";
//NSLog(gender);
}
else {
contactType = @"b";
//NSLog(gender);
}
}
-(IBAction)saveData:(id)sender
{
NSMutableArray *array = [[NSMutableArray alloc] init];
[array removeAllObjects];
[array addObject:field1.text];
[array addObject:field2.text];
[array addObject:gender];
[array addObject:contactType];
[array writeToFile:[self dataFilePath] atomically:YES];
[array release];
}
-(NSString *)dataFilePath
{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [path objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFilename];
}
-(void)viewWillAppear:(BOOL)animated
{
NSString *filePath = [self dataFilePath];
if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
field1.text = [array objectAtIndex:0];
field2.text = [array objectAtIndex:1];
gender = [array objectAtIndex:2];
contactType = [array objectAtIndex:3];
if([gender isEqualToString:@"Male"])
{
genderSegment.selectedSegmentIndex = 0;
}
else
{
genderSegment.selectedSegmentIndex = 1;
}
if([contactType isEqualToString:@"a"])
{
}
else
{
}
[array release];
}
}
-(IBAction)textDone:(id)sender
{
[sender resignFirstResponder];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[gender release]; //need to release
[contactType release]; //need to release
[field1 release]; //need to release
[field2 release]; //need to release
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
gender = @"Male";
contactType = @"a";
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
gender = nil; //need to release
contactType = nil; //need to release
field1 = nil ; //need to release
field2 = nil; //need to release
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
答案 0 :(得分:3)
您没有初始化gender
财产。你需要:
- (void)viewDidLoad
{
[super viewDidLoad];
self.gender=[[NSString alloc]init];
self.gender = @"Male";
//so something here
// Do any additional setup after loading the view from its nib.
}
注意:如果您使用@synthesize
编译器指令,则需要在任何地方使用自带点表示法来引用属性,例如self.gender
。