0

我试图学习如何使用 NSKeyedArchiver 保存对象数组,我编写了一个小应用程序来做到这一点,我登录查看数组是否已保存,但每次我得到 0 的数组计数,这里是代码。

视图控制器.h

@interface ViewController : UIViewController
{
    IBOutlet UITextField *text;
    IBOutlet UITextField *textName;
    IBOutlet UITextField *textAge;
    IBOutlet UILabel *name;
    IBOutlet UILabel *age;

    BOOL flag;
    BOOL choice;
    NSString *documentDirectory;
    NSMutableArray *anArray;
    Person *p;
    NSData *data;

}
-(BOOL) dataFilePath;
-(IBAction)readPlist;
-(IBAction) writePlist;


@property (strong,nonatomic)IBOutlet UITextField *text;
@property (strong,nonatomic)IBOutlet UITextField *textName;
@property (strong,nonatomic)IBOutlet UITextField *textAge;
@property (strong,nonatomic)IBOutlet UILabel *name;
@property (strong,nonatomic)IBOutlet UILabel *age;
@property (strong,nonatomic)NSString *documentDirectory;
@property (strong,nonatomic)NSMutableArray *anArray;
@end

视图控制器.m

    @interface ViewController ()

    @end

    @implementation ViewController
    @synthesize text,documentDirectory,textAge,textName,name,age,anArray;


    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // checking if the file was created and show a message if its created or not.
        if ([self dataFilePath]) {
            NSLog(@"File Created !");
        } else {
            NSLog(@"File Not Created !");
        }
        NSLog(@"File location : %@",documentDirectory);
        choice = YES;




        // Do any additional setup after loading the view, typically from a nib.

    }

    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }


    -(BOOL) dataFilePath
    {
        NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        documentDirectory = [path objectAtIndex:0];
        documentDirectory = [documentDirectory stringByAppendingPathComponent:@"Milmers­Data.dat"];

        return TRUE;
    }

    - (IBAction)writePlist
    {
        p.name = textName.text;
        p.age = [textAge.text intValue];

        [anArray addObject:p];

        for (int i=0; i<[anArray count]+1; i++) {
         Person *pp = [[Person alloc]init];
         pp=[anArray objectAtIndex:i];
         NSLog(@"Name: %@",pp.name); // checking the names in pp object but getting null
    }
        data = [NSKeyedArchiver archivedDataWithRootObject:anArray];
        [data writeToFile:documentDirectory options:NSDataWritingAtomic error:nil];
        NSLog(@"Array length: %d",[anArray count]); //Always got array count zero.


    }

    -(IBAction)readPlist
    {


        NSString *filePath = documentDirectory;
        NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
        NSLog(@"The array is: %@",array); // it shows that there is nothing in the array


    }

    @end

我最初编写了用于编写 .plist 文件的类,但后来我知道我不能将对象存储在 .plist 文件中,所以我尝试使用存档,这就是方法名称中有 plist 的原因。

先感谢您

4

3 回答 3

0

尝试在 viewDidLoad 中添加它

[[NSFileManager defaultManager] createFileAtPath:documentDirectory contents:nil error:nil];

看起来你从来没有这样做过,并且只有在文件已经存在时才使用存档写入文件(确保你只这样做一次,否则每次加载该视图时,文件中的所有数据都将被清空) . 当你这样做时

if ([self dataFilePath])

这是没有意义的,因为无论文件是否存在,它总是返回是。

于 2013-05-27T18:16:49.363 回答
0

你的 Person 类是否实现了 NSCoding?具体来说,您需要在 Person.m 中实现类似以下内容:

- (id)initWithCoder:(NSCoder *)decoder {
  self = [super init];
  if (!self) {
    return nil;
  }

  self.name = [decoder decodeObjectForKey:@"name"];
  self.age = [decoder decodeObjectForKey:@"age"];

  return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder {
  [encoder encodeObject:self.name forKey:@"name"];
  [encoder encodeObject:self.age forKey:@"age"];
}
于 2013-05-27T18:19:26.420 回答
0

看起来您从未创建p要添加到数组的实例。尝试:

    Person *p = [[Person alloc] init];
    p.name = textName.text;
    p.age = [textAge.text intValue];

    [anArray addObject:p];

你的索引限制在这个循环中也是错误的

    for (int i=0; i<[anArray count]; i++) {
        NSLog(@"Name: %@", [[anArray objectAtIndex:i] name]);
    }

你真的应该看到几个不同的崩溃......

于 2013-05-27T17:51:46.803 回答