0

我正在尝试使用NsKeyedArchiever保存包含型号、品牌、年份、颜色、vin 的汽车详细信息。代码如下:

void CarData()
{
    Car *myCar = [[Car alloc] init];
    myCar.model = [carTempArray objectAtIndex:0];//Temporary array contains value
    myCar.make = [carTempArray objectAtIndex:1];
    myCar.color = [carTempArray objectAtIndex:2];
    myCar.year = [carTempArray objectAtIndex:3];
    myCar.vin = [NSNumber numberWithInt:check];
    NSString *docspath = [NSHomeDirectory() stringByAppendingPathComponent:@"documents"];
    BOOL saved =[NSKeyedArchiver archiveRootObject: myCar toFile:docspath];
    NSLog(@"did save state %d",saved);
    [myCar release];
    [NSKeyedArchiver release];
}

之后使用NSkeyedUnarcheiver如下:

void ShowData()
{
    NSString *docspath = [NSHomeDirectory() stringByAppendingPathComponent:@"documents"]; 

        Car * retrieveCar = [NSKeyedUnarchiver unarchiveObjectWithFile:docspath];
       // for(int i=0 ; i<studentList.count;i++)
        {
          //  Car * retrieveCar =[studentList objectAtIndex:0];

    NSString * first  = retrieveCar.model;
    NSString * first1 = retrieveCar.make;
    NSString * first2 = retrieveCar.color;
    NSString * first3 = retrieveCar.year;
    NSLog(@"%@",first );
    NSLog(@"%@",first1 );

    NSLog(@"%@",first2 );
    NSLog(@"%@",first3 );
}

但这只取一个值并使用一个值。我想将NSKeyedArchiever对象保存在一个数组中,以便多辆汽车可以使用相同的代码成为它的一部分。并且加载与上面相同。请纠正问题。我想运行多辆汽车的代码,我能够在汽车阵列中执行以下任务,例如搜索汽车、删除汽车记录和添加汽车。

4

0 回答 0