我确实使tableview保存并加载数据。我有2个警告和2个错误。
错误:
指向整数转换的不兼容指针从'NSString * __ strong'分配给'BOOL'(又名'signed char') ARC备不允许将'BOOL'(又名'signed char')隐式转换为'id' 不兼容的整数到指针转换将'BOOL'(又名'signed char')发送到'id'类型的参数
我哪里错了?我该如何解决?
Task.h
#import <Foundation/Foundation.h>
@interface Task : NSObject
@property (nonatomic,strong) NSString *name;
@property (nonatomic,assign) BOOL done;
-(id)initWithName:(NSString *)name done:(BOOL)done;
@end
Task.m
#import "Task.h"
@implementation Task
@synthesize name = _name;
@synthesize done = _done;
-(id)initWithName:(NSString *)name done:(BOOL)done {
self = [super init];
if (self) {
self.name = name;
self.done = done;
}
return self;
}
我的保存并加载代码
- (void)applicationDidEnterBackground:(NSNotification *)notification {
NSLog(@"Entering Background");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// paths[0];
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
//NSArray *keys = [[NSArray alloc] initWithObjects:@"task", nil];
NSMutableArray *array = [[NSMutableArray alloc] init];
NSEnumerator *enumerator = [_tasks objectEnumerator];
Task *tempTodo;
while ( tempTodo = [enumerator nextObject])
{
[array addObject:tempTodo.name];
[array addObject:tempTodo.done]; //Eror is here..
}
[array writeToFile:plistPath atomically:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tasks = [[NSMutableArray alloc] init];
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:app];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// paths[0];
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
if ([fileManager fileExistsAtPath:plistPath] == YES)
{
NSMutableArray *readArray = [NSMutableArray arrayWithContentsOfFile:plistPath];
_tasks = [[NSMutableArray alloc] init];
NSEnumerator *enumerator = [readArray objectEnumerator];
NSString *str = [[NSString alloc] init];
while ( str = [enumerator nextObject])
{
Task *tempTodo = [[Task alloc] init];
tempTodo.name = str;
str = [enumerator nextObject];
tempTodo.done = str; //Error and warning is here.
[_tasks addObject:tempTodo];
}
[[self tableView] reloadData];
}
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"AddTaskSegue"]) {
UINavigationController *navCon = segue.destinationViewController;
AddTaskViewController *addTaskViewController = [navCon.viewControllers objectAtIndex:0];
addTaskViewController.taskListViewController = self;
} else if ([segue.identifier isEqualToString:@"EditDoneTaskSegue"] || [segue.identifier isEqualToString:@"EditNotDoneTaskSegue"]) {
EditTaskViewController *edit =segue.destinationViewController;
edit.task = [self.tasks objectAtIndex:self.tableView.indexPathForSelectedRow.row];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *NotDoneCellIdentifier = @"NotDoneTaskCell";
static NSString *DoneCellIdentifier = @"DoneTaskCell";
Task *currentTask = [self.tasks objectAtIndex:indexPath.row];
NSString *cellIdentifier = currentTask.done ? DoneCellIdentifier : NotDoneCellIdentifier;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text = currentTask.name;
return cell;
}
答案 0 :(得分:0)
<击>
而不是@property (nonatomic,assign) BOOL done;
使用
@property BOOL done;
击> <击> 撞击>
此外,
NSString *str = [[NSString alloc] init];
while ( str = [enumerator nextObject])
{
Task *tempTodo = [[Task alloc] init];
tempTodo.name = str;
str = [enumerator nextObject];
tempTodo.done = str; //Error and warning is here.
此处str
为字符串,您将其分配给BOOL
。
和
while ( tempTodo = [enumerator nextObject])
{
[array addObject:tempTodo.name];
[array addObject:tempTodo.done]; //Eror is here..
}
NSArray
只能在tempTodo.done
为BOOL
时才包含对象signed char
这是一种基本类型[array addObject:@(tempTodo.done)]; //NSNumber
您可以将该内容打包为字符串或数字
{{1}}