1

NSMutableData 中是否有层次结构系统?如果是这样,我如何以分层方式加载信息?例子

//Create path to saving location
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSMutableData *gameData;
NSKeyedUnarchiver *decoder;
NSString *documentPath = [documentsDirectory stringByAppendingPathComponent:@"tierSave.dat"];
gameData = [NSData dataWithContentsOfFile:documentPath];
//end

//start loading
    for(NSArray* firstData in gameData){

    decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:gameData];

    int level1 = [decoder decodeIntegerForKey:level1];
    }
//end

但是在 for(NSArray* firstData in gameData) 中,编译器告诉我 gameData 不会响应计数,因为它是 NSMutableData 而不是 NSMutableArray 等,我该如何重新实现上面的代码?

4

1 回答 1

1

保存:

  • 使用多个 NSData 创建一个 NSArray
  • 使用 NSKeyedArchiver 从你的 NSArray 创建一个 NSData

加载:

  • 从文件加载 NSData
  • 使用 NSKeyedUnarchiver 提取 NSArray
  • 循环你的 NSArray

在代码中: NSData *data0 = [NSData 数据]; // 数据 1 NSData *data1 = [NSData 数据]; // 数据 2

/////////// SAVE
// now make a array
NSArray *array = [NSArray arrayWithObjects:data0, data1, nil];

// now archive the data
NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject:array];
// archivedData contains now a binary plist with your archived array including your NSData objects

[archivedData writeToFile:path atomically:YES];



/////// LOAD
NSData *archivedDataFromFile = [NSData dataWithContentsOfFile:path];

NSArray *newArray = [NSKeyedUnarchiver unarchiveObjectWithData:archivedDataFromFile];

NSData *dataLoaded0 = [newArray objectAtIndex:0];
于 2012-04-04T08:52:05.247 回答