[英]Save TableView Data
I do make tableview save and load data. 我确实使tableview保存和加载数据。 I have 2 warning and 2 error. 我有2警告和2错误。
Errors: 错误:
Incompatible pointer to integer conversion assigning to 'BOOL' (aka 'signed char') from 'NSString *__strong' 整数转换的不兼容指针从'NSString * __ strong'分配给'BOOL'(aka'signed char')
Implicit conversion of 'BOOL' (aka 'signed char') to 'id' is disallowed with ARC ARC不允许将“ BOOL”(又名“ signed char”)隐式转换为“ id”
Incompatible integer to pointer conversion sending 'BOOL' (aka 'signed char') to parameter of type 'id' 指针转换不兼容的整数,向“ id”类型的参数发送“ BOOL”(又名“ signed char”)
Where is my wrong? 我哪里错了? And How do I fix? 以及如何解决?
Task.h 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 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;
}
My Save and Load Code 我的保存和加载代码
- (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;
}
Instead of
@property (nonatomic,assign) BOOL done;
代替
@property (nonatomic,assign) BOOL done;
use
使用
@property BOOL done;
Also, 也,
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.
Here str
is string and you assigning it to BOOL
. 在这里, str
是字符串,您将其分配给BOOL
。
And 和
while ( tempTodo = [enumerator nextObject]) { [array addObject:tempTodo.name]; [array addObject:tempTodo.done]; //Eror is here.. }
NSArray
can contain objects only while tempTodo.done
is BOOL
which is a primitive type signed char
仅当tempTodo.done
是BOOL
, NSArray
才能包含对象, BOOL
是原始类型的带signed char
You can box that thing to string or number as 您可以将该东西装箱成字符串或数字作为
[array addObject:@(tempTodo.done)]; //NSNumber
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.