Iphone 是否可以在info.plist中存储2D数组
我有如下9x9矩阵阵列。我想将其作为游戏参数本地存储在我的iPhone应用程序中Iphone 是否可以在info.plist中存储2D数组,iphone,objective-c,xcode,Iphone,Objective C,Xcode,我有如下9x9矩阵阵列。我想将其作为游戏参数本地存储在我的iPhone应用程序中 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
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
文件中?如果有其他方法,请提出建议。您肯定不想将其存储在您的Info.plist中。正如前面所说的,这是关于应用程序的信息,而不是数据存储。相反,在应用程序的资源中放置一个单独的文件,并将数据保存在其中
您可以将数据转换为NSArray
中的NSNumbers
的二维数组,并将该NSArray
写入一个单独的plist文件,但这会带来很大的开销
相反,如果您已经知道数组的大小始终为9x9,我建议将数组传递给NSData
对象,并将其保存到文件中
NSData *arrayData = [NSData dataWithBytes:str2Darray length:sizeof(int)*9*9];
[arrayData writeToFile:dataPath atomically:YES];
如果此数组表示游戏在任何时候的状态,并且您希望保存它,以便用户可以在返回应用程序时继续,那么您应该将其保存到documents文件夹中
NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentsDirectory, NSUserDomainMask, YES);
NSString *dataPath = [docPath stringByAppendingPathComponent:@"MyArrayData.txt"];
如果它代表级别数据,或者您希望随游戏附带的游戏的开始配置,那么我建议启动一个临时的简单第二个项目。执行一次,抓取保存的文件并将其放入项目资源中
然后,您可以通过以下路径访问它:
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];
你漏掉了更难猜的一点。。。把数据拿回来<代码>[data getBytes:&str2Darray length:data.length]代码>这真的很有帮助..谢谢