每次重新启动iPhone应用程序时,NSHomeDirectory都会返回不同的路径 [英] NSHomeDirectory returns different path each time iPhone App is restarted

查看:214
本文介绍了每次重新启动iPhone应用程序时,NSHomeDirectory都会返回不同的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到NSHomeDirectory每次使用Xcode重新启动应用程序时返回不同的路径,显然即使我手动点击图标,因为它没有加载文件的内容。每次重新启动应用程序时,我都惊呆了它给了我一个不同的目录。这种情况发生在模拟器和设备上,即使我使用ForUser方法。我的调用如下所示:

I noticed that NSHomeDirectory returns a different path each time i restart the App with Xcode, and apparently even if i click the icon manually since it doesn't load the file's contents. I'm stunned that it gives me a different directory each time i restart the app. This happens on both simulator and device and even if i use the "ForUser" method. My calls look like this:

NSString* fullPath = [NSHomeDirectory() stringByAppendingPathComponent:@"test.file"];

NSString* fullPath = [NSHomeDirectoryForUser(@"MrX") stringByAppendingPathComponent:@"test.file"];

甚至

NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [docsDirectory stringByAppendingPathComponent:@"test.file"];

两个应用程序启动的日志如下所示,注意GUID部分的变化:

And the log of two app starts looks like this, notice how the GUID part has changed:

loadCharacters: /var/mobile/Applications/ABE7E33E-439B-4258-8FC1-127A3CD00D87/test.file
loadCharacters: /var/mobile/Applications/71C02507-6347-4693-8CC1-537BE223179E/test.file

什么我做错了,或者我错过了什么?

What am i doing wrong, or what am i missing?

解决方案:

起初我保存到NSHomeDirectory本身,而不是Documents子目录。由于App目录随每个部署而更改,但只复制了Documents目录中的文件,因此每次部署后保存的文件都会丢失。然后我使用了Documents文件夹,但是有人建议我应该把它放在Documents的子文件夹中,我只是这样做但是忘了创建文件夹,因此文件没有保存。我在这一次失败了。 :)

At first i was saving to NSHomeDirectory itself, not the "Documents" subdirectory. Since the App directory changes with each deployment but only the files in the "Documents" directory get copied, the saved file was lost after each deployment. Then i used the "Documents" folder but someone suggested i should probably put it in a subfolder of "Documents" i did just that but forgot to create the folder, hence the file wasn't saved. I double-failed on this one. :)

只需保存到NSHomeDirectory()+Documents / test.file即可。

Simply saving to NSHomeDirectory() + "Documents/test.file" works.

推荐答案

你正确地做到了这一点。每次将应用程序安装到模拟器或设备中时,都会将其放入不同的目录中(我不知道为什么,但这就是发生的情况)。但是,其所有目录结构都移动到新位置。因此,您的RELATIVE路径将保持不变。

You're doing this correctly. Each time an app is installed into the simulator or device, it's placed into a different directory (I'm not sure why, but that's what happens). However, all of its directory structure is moved to the new position. Thus, your RELATIVE paths will remain unchanged.

这篇关于每次重新启动iPhone应用程序时,NSHomeDirectory都会返回不同的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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