iPhone 应用程序从文件内容中抛出 EXC_BAD_ACCESS 和字典

发布于 2024-08-30 12:48:15 字数 825 浏览 2 评论 0原文

我在应用程序的 applicationDidFinishLaunching 方法中包含代码

 NSArray *paths = [[NSArray alloc]   
initWithArray:NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)]; 

NSString *docsDirectory = [[NSString alloc] initWithString:[paths objectAtIndex:0]];

NSLog(@"This app's documents directory: %@",docsDirectory);

NSString *docsDirectoryWithPlist = [[NSString alloc] initWithFormat:@"%@/Stuff.plist", docsDirectory];



BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:docsDirectoryWithPlist isDirectory:NO];

if (fileExists) 
{
    chdir([docsDirectory UTF8String]);
    NSMutableDictionary *readDict = [[NSMutableDictionary alloc] initWithContentsOfFile:@"Stuff.plist"];

,每当到达最后一行时,它就会崩溃,并一路抛出 EXC_BAD_ACCESS

提前致谢!

I have the code

 NSArray *paths = [[NSArray alloc]   
initWithArray:NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)]; 

NSString *docsDirectory = [[NSString alloc] initWithString:[paths objectAtIndex:0]];

NSLog(@"This app's documents directory: %@",docsDirectory);

NSString *docsDirectoryWithPlist = [[NSString alloc] initWithFormat:@"%@/Stuff.plist", docsDirectory];



BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:docsDirectoryWithPlist isDirectory:NO];

if (fileExists) 
{
    chdir([docsDirectory UTF8String]);
    NSMutableDictionary *readDict = [[NSMutableDictionary alloc] initWithContentsOfFile:@"Stuff.plist"];

in an application's applicationDidFinishLaunching method and whenever it gets to the last line it crashes, throwing EXC_BAD_ACCESS along the way.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈年往事 2024-09-06 12:48:15
NSMutableDictionary *readDict = [[NSMutableDictionary alloc] initWithContentsOfFile:@"Stuff.plist"];

此行尝试从捆绑目录中读取文件。从完整路径读取文件:

NSMutableDictionary *readDict = [[NSMutableDictionary alloc] initWithContentsOfFile:docsDirectoryWithPlist];
NSMutableDictionary *readDict = [[NSMutableDictionary alloc] initWithContentsOfFile:@"Stuff.plist"];

This line tries to read file from the bundle directory. Read file from full path:

NSMutableDictionary *readDict = [[NSMutableDictionary alloc] initWithContentsOfFile:docsDirectoryWithPlist];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文