目标C文档目录在设备上不断变化 [英] Objective C documents directory keeps changing on device

查看:81
本文介绍了目标C文档目录在设备上不断变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从一开始我就一直在为iOS开发,但遇到了一些麻烦.我开始制作一个需要创建文件并将其保存到documents文件夹和应用程序支持的应用程序,但是使用

I've been developing for iOS since the beginning and I've run into some trouble. I started making an app that needs to create files and save them to the documents folder and application support, however the good ol' standard way of using the

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentsDirectory, NSUserDomainMask, YES);
NSString *filePath = paths[0];

不起作用.每次启动应用程序时,它都会不断显示不同的文件路径.

Doesn't work. It keeps bringing up a different file path every time the app is launched.

我已经在Google上搜索了此内容,到目前为止,每个人似乎都只说他们在模拟器上遇到了这个问题.但是,这不是仅限于模拟器的问题,无论您是否从Xcode运行它,每次启动应用程序时,它都会在设备上发生. 其他搜索结果只是告诉我这样做的旧方法和/或来自很多年前.

I've searched on google about this and everyone so far seems to just say they have this problem on the simulator. However, this is not a problem restricted to the simulator, it happens on the device every time the app is started, regardless of whether or not you run it from Xcode or not. Other search results just tell me the old way of doing it and/or are from too many years ago.

有人知道当前写入Documents and Application Support文件夹的方法是吗? 这是使用iOS SDK8.1在Xcode 6.1上编译并在iOS7.1和iOS8.1上运行时出现的问题.

Does anyone know the current way to write to the Documents and Application Support folders is? This is a problem compiling from Xcode 6.1, using the iOS SDK8.1, running on both iOS7.1 and iOS8.1

任何帮助将不胜感激

推荐答案

如果要在永久性存储中存储路径,并打算在将来某个日期使用该路径,则应仅存储相对于应用程序文档的路径文件夹,然后每当需要访问该资源时,在运行时获取Documents文件夹路径并附加资源的相对路径.

If you are storing paths in persistent storage with the intent to use that path at some date in the future, you should store only the path relative to the app's Documents folder and then whenever you need to access that resource, get the Documents folder path at runtime and append your relative path of your resource.

顺便说一句,当我通过Xcode运行我的应用程序时,我只会看到这种变化的沙箱路径行为.如果我直接从设备运行应用程序,则路径不变.无论如何,我总是倾向于使用相对路径而不是固定路径.我永远不会假设沙箱路径无法更改(例如,如果稍后备份并还原了应用,则我不会假设路径未更改).

As an aside, I only see this changing sandbox path behavior when I run my app via Xcode. If I run the app directly from my device, the path is unchanged. Regardless, I'd always be inclined to use relative paths rather than fixed paths. I would never assume that the sandbox path couldn't change (e.g., if app is backed up and restored later, I wouldn't assume that the paths are unchanged).

这篇关于目标C文档目录在设备上不断变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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