我创建了一个涉及使用字典的游戏。它有大约 73000 个单词。但是,在审核过程之后,该应用程序被拒绝了。以下是评论意见:
“我们发现您的应用程序在运行 iOS 6.1.3 的 iPad 上崩溃了” “点击播放按钮时应用程序崩溃。” “如果您的应用使用了太多内存,它可能会遇到此问题。”
我被难住了。在我运行 6.1.3 的 iPhone 或运行 6.1.3 的 iPad 上的应用程序没有问题。所以我假设它在 iPad mini 上崩溃了。有没有更有效的方法/更好的地方将字典加载到内存中?这就是我目前在按下播放按钮后的做法。提前致谢!
NSString *filePath = [[NSBundle mainBundle] pathForResource: [NSString stringWithFormat: @"dictionary"] ofType:@"txt"]; //set where to get the dictionary from
NSData* data = [NSData dataWithContentsOfFile:filePath]; //pull the content from the file into memory
NSString* string = [[[NSString alloc] initWithBytes:[data bytes]
length:[data length]
encoding:NSUTF8StringEncoding] autorelease];//convert the bytes from the file into a string
NSString* delimiter = @"\n";//split the string around newline characters to create an array
currentDict = [string componentsSeparatedByString:delimiter];
[currentDict retain];
最佳答案
在我看来,73,000 个单词不会对内存造成压力,它可能完全是另外一回事。运行 Instruments,使用分配工具来监控你的内存发生了什么。确定是否真的是这本词典有问题。
也就是说,出于多种原因(包括您建议的原因),您使用的策略并不理想。与当前方法相比,您可以使用多种替代方法在任何给定时间获取所需的字典部分,据我所知,当前方法始终完全驻留在内存中。
一个简单的解决方案可能是转换为 NSDictionary 并从文件加载它。另一个更复杂但更有效的解决方案是推出一个预先填充的 CoreData 数据库(例如,如果您在 github 上窥探一下,有许多现有的解决方案)。
关于iphone - 将字典加载到应用程序中的最佳方式? iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16598521/