【发布时间】:2013-05-16 22:25:41
【问题描述】:
我创建了一个涉及使用字典的游戏。它有大约73000字。然而,在审查过程之后,该应用程序被拒绝了。这是评论 cmets:
“我们发现您的应用在运行 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];
【问题讨论】:
-
包含您的文字的文件大小是多少?如果它小于大约 1,000,000 字节,我怀疑您的问题出在其他地方。 (他们有没有向您发送任何痕迹或转储信息?)
-
您是否在其上运行过任何分析软件? XCode 附带的工具工具有一个称为分配的工具,可以让您深入了解内存的去向...
-
文件为 1,745,228 字节。这有什么不同吗?
-
你真的需要加载整个东西吗?无论如何,您当然应该配置应用程序。
-
为什么不直接使用
stringWithContentsOfFile:encoding:error:而不是先创建一个NSData对象,然后再将其转换为NSString。这将减少临时内存使用量(约 50%),尽管我怀疑这真的是这里的主要问题。二进制 plist 会更好,因为这样您就不必在每次加载时都拆分字符串。