Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

iphone - Is it possible to store 2D array in info.plist

I have 9x9 matrix array as follow. I want to store it in my iPhone app locally as game parameters.

int str2Darray[9][9] = { 

    {-1, -1, -1, 1, 1, 1, -1, -1, -1}, 
    {-1, -1, -1, 1, 1, 1, -1, -1, -1}, 
    {-1, -1, -1, 1, 1, 1, -1, -1, -1}, 
      {1, 1, 1, 1, 1, 1, 1, 1, 1}, 
      {1, 1, 1, 1, 0, 1, 1, 1, 1}, 
      {1, 1, 1, 1, 1, 1, 1, 1, 1}, 
    {-1, -1, -1, 1, 1, 1, -1, -1, -1}, 
    {-1, -1, -1, 1, 1, 1, -1, -1, -1}, 
    {-1, -1, -1, 1, 1, 1, -1, -1, -1}, 

};

Is it possible to store above array in .plist file? Please suggest any other way also, if any.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You most certainly don't want to store that in your Info.plist. As the same already says, that's for information about your app, not data storage. Instead put a separate file in your app's resources and save your data in there.

You could transform your data into a 2D array of NSNumbers in NSArrays and write that NSArray into a separate plist file, but that's a lot of overhead.

Instead, if you already know that the array's size will always be 9x9, I recommend passing the array to an NSData object and save that into a file.

NSData *arrayData = [NSData dataWithBytes:str2Darray length:sizeof(int)*9*9];
[arrayData writeToFile:dataPath atomically:YES];

If this array represents the state of your game at any point and you want to save it, so that the user can continue when he returns to your app, then you should save it to your documents folder.

NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentsDirectory, NSUserDomainMask, YES);
NSString *dataPath = [docPath stringByAppendingPathComponent:@"MyArrayData.txt"];

If it represents level data, or the start configuration of your game that you want to ship with your game, then I recommend starting a temporary simple second project. Execute it once, grab the saved file and put it into your projects resources.

You can then access it at this path:

NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"MyArrayData" ofType:@"txt"];
int length = sizeof(int)*9*9;
str2DArray = (int *)malloc(length); // Assuming str2Darray is an ivar or already defined.
NSData *arrayData = [NSData dataWithContentsOfFile:dataPath];
[arrayData getBytes:&str2Darray length:length];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...