是否可以在info.plist中存储2D数组 [英] Is it possible to store 2D array in info.plist

查看:47
本文介绍了是否可以在info.plist中存储2D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有9x9矩阵数组,如下所示.我想将它作为游戏参数存储在我的i​​Phone应用程序中.

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}, 

};

是否可以将上述数组存储在 .plist 文件中?如果有的话,也请提出其他建议.

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

推荐答案

您当然不希望将其存储在Info.plist中.就像已经说过的那样,这是关于您的应用程序的信息,而不是数据存储.而是将一个单独的文件放在应用程序的资源中,然后将数据保存在其中.

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.

您可以将数据转换为 NSArrays 中的 NSNumbers 的二维数组,并将该 NSArray 写入单独的plist文件中,但这是一个很多开销.

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.

相反,如果您已经知道数组的大小始终为9x9,建议您将数组传递给 NSData 对象,然后将其保存到文件中.

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];

这篇关于是否可以在info.plist中存储2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆