有一个initWithCoder方法调用两个属性(文本,已选中),但由于某种原因,无法看到它们。已注释错误消息。
ChecklistViewController.m
#import "ChecklistsViewController.h"
#import "ChecklistItem.h"
@interface ChecklistsViewController ()
@end
@implementation ChecklistsViewController {
NSMutableArray *_items;
}
-(NSString *)documentsDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths firstObject];
return documentsDirectory;
}
-(NSString *)dataFilePath
{
return [[self documentsDirectory] stringByAppendingPathComponent:@"Checklists.plist"];
}
-(void)saveChecklistItems
{
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:_items forKey:@"ChecklistItems"];
[archiver finishEncoding];
[data writeToFile:[self dataFilePath] atomically:YES];
}
-(void)loadChecklistItems
{
NSString *path = [self dataFilePath];
if ([[NSFileManager defaultManager]fileExistsAtPath:path]) {
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
_items = [unarchiver decodeObjectForKey:@"ChecklistItems"];
[unarchiver finishDecoding];
} else {
_items = [[NSMutableArray alloc] initWithCapacity:20];
}
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super init])) {
self.text = [aDecoder decodeObjectForKey:@"Text"]; //Property 'text' not found on 'ChecklistsViewController'
self.checked = [aDecoder decodeBoolForKey:@"Checked"]; //Property 'checked' not found on 'ChecklistsViewController'
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
ChecklistItem.h
#import <Foundation/Foundation.h>
@interface ChecklistItem : NSObject <NSCoding>
@property (nonatomic, copy) NSString *text;
@property (nonatomic, assign) BOOL checked;
-(void)toggleChecked;
@end
答案 0 :(得分:1)
您的initWithCoder:
方法位于名为ChecklistsViewController
的类中,self
引用ChecklistsViewController
实例。但text
和checked
属性是ChecklistItem
类的成员。