Objective c Xcode保存NSMutableArray的字符串&;布尔值
我在min设计的一个应用程序有点小问题,但我是一个完全的初学者,首先是关于我要完成什么的一些信息 我有一个plist,它填充一个NSMutableArray,其中包含许多值,每个值中都有一个字符串和一个BOOL,我可以让程序在打开应用程序时保存一份文件副本,并将数据与复选标记的accessoryview一起加载到tableview中 现在,复选标记工作正常,您可以选择不同的项目,并且复选标记仅显示在这些项目上,其他项目均不显示。如果您检查日志,则每个项目的详细信息复选框都会更改,但当我再次保存时,复选标记状态不会保留,因为当我再次打开应用程序时,它只会保存每次都是0 这是我的一些代码,任何帮助都将不胜感激 谢谢 布拉德Objective c Xcode保存NSMutableArray的字符串&;布尔值,objective-c,xcode,nsmutablearray,Objective C,Xcode,Nsmutablearray,我在min设计的一个应用程序有点小问题,但我是一个完全的初学者,首先是关于我要完成什么的一些信息 我有一个plist,它填充一个NSMutableArray,其中包含许多值,每个值中都有一个字符串和一个BOOL,我可以让程序在打开应用程序时保存一份文件副本,并将数据与复选标记的accessoryview一起加载到tableview中 现在,复选标记工作正常,您可以选择不同的项目,并且复选标记仅显示在这些项目上,其他项目均不显示。如果您检查日志,则每个项目的详细信息复选框都会更改,但当我再次保存时
BOOL
不是对象,而是基本类型。因此,它不能(正确地)保存在数组或字典中。您需要使用NSNumber
类来包装它:
[NSNumber numberWithBool:checked]//这应该添加到字典中
我是从手机上写的,所以我真的看不到你所有的代码。但我只是想说,您试图实现的目标可能可以通过使用NSUserdefaults而不是保存文件来解决。你调查过了吗
哦,就像埃文说的,布尔不是一个物体。数组中只能存储对象 将单元格添加到词典中有什么原因吗?UITableViewCell不是与属性列表兼容的对象,因此它可能会使数组无法正确保存。请添加更改复选框值的代码如何使用NSUserDefaults完成此操作我想我已经尝试过了,但没有成功,因为我说我是一个绝对的初学者。谢谢
- (void)viewDidLoad {
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"CustomChecklist.plist"];
success = [fileManager fileExistsAtPath:filePath];
NSLog(@"STATUS OF SUCCESS %d",success);
if (!success) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"OriginalChecklist" ofType:@"plist"];
success = [fileManager copyItemAtPath:path toPath:filePath error:NULL];
self.dataArray = [NSMutableArray arrayWithContentsOfFile:filePath];
NSLog(@"IF STATEMENT CREATING THE FILE");
}else {
self.dataArray = [NSMutableArray arrayWithContentsOfFile:filePath];
NSLog(@"IF STATEMENT READING THE FILE");
}
NSLog(@"location information %@", filePath);
[super viewDidLoad];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *kCustomCellID = @"MyCellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCustomCellID];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}
NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row];
cell.textLabel.text = [item objectForKey:@"text"];
[item setObject:cell forKey:@"cell"];
BOOL checked = [[item objectForKey:@"checked"] boolValue];
UIImage *image = (checked) ? [UIImage imageNamed:@"checked.png"] : [UIImage imageNamed:@"unchecked.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.frame = frame; // match the button's size with the image size
[button setBackgroundImage:image forState:UIControlStateNormal];
// set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet
[button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
return cell;
}
- (void)viewWillDisappear:(BOOL)animated
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savePath = [documentsDirectory stringByAppendingPathComponent:@"CustomChecklist.plist"];
NSLog(@"View Will Disappear SAVE location information %@", savePath);
[dataArray writeToFile:savePath atomically:YES];
}