【问题标题】:Save TableView Data保存 TableView 数据
【发布时间】:2013-03-16 11:28:39
【问题描述】:

我确实让 tableview 保存和加载数据。我有 2 个警告和 2 个错误。

错误:

从“NSString *__strong”分配给“BOOL”(又名“有符号字符”)的整数转换指针不兼容
ARC 不允许将“BOOL”(又名“signed char”)隐式转换为“id”
不兼容的整数到指针转换将“BOOL”(又名“signed char”)发送到“id”类型的参数

我哪里错了?我该如何解决?

任务.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

任务.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;
}

【问题讨论】:

    标签: ios objective-c core-foundation


    【解决方案1】:

    而不是@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.doneBOOL,这是一个基本类型 signed char

    你可以把那个东西装箱成字符串或数字

    [array addObject:@(tempTodo.done)]; //NSNumber
    

    【讨论】:

    • @property 行没有问题。而在 iOS 中,默认值为 atomic,因此通过删除 nonatomic,您将改变其含义。
    • @rmaddy:好的,我打了那些线。
    • tempTodo.done = BOOL;部分有错误。错误:意外的类型名称“BOOL”:预期的表达式
    • @SalihÇiftçi:您需要使用“是”或“否”。 BOOL 是一种数据类型
    • 天哪。我用的是,然后一切都完成了。如果是,那么是的。如果没有,那么没有。我如何能?我的英语不好。希望你能理解。
    猜你喜欢
    • 1970-01-01
    • 2018-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    相关资源
    最近更新 更多