写入捆绑文件? [英] Writing to files in bundle?

查看:51
本文介绍了写入捆绑文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您在此链接,可变路径必须是磁盘上的文件吗?可以写入捆绑中的文件,还是必须始终先部署它?

If you scroll down to the section 'Writing to Files and URLs' at this link, would the path varaible have to be a file on disk? Is it possible to write to a file in the bundle or must it always be deployed first?

推荐答案

您可以将文件写入到应用程序包随心所欲。只要通过NSBundle的 pathForResource:ofType:方法获取文件的路径,就可以写入该文件。如果只需要捆绑软件的目录,请使用 resourcePath

You can write files to the application bundle as much as you'd like. Just get the path of the file through NSBundle's pathForResource:ofType: method and you can write to that file. If you want just the directory of the bundle, use resourcePath.

您不希望 来执行此操作。原因多种多样,但是您会破坏代码签名,这是一个很大的原因。您应该改用已建立的约定(例如写给Library:Application Support:Your App)。

You don't want to do this, though. There are various reasons, but you'll break code signing, which is a big one. You should use the established conventions instead (such as writing to Library:Application Support:Your App).

EDIT:对于(可能)更令人信服的理由是不这样做。当我第一次学习Cocoa编程时,我之所以保存在捆绑包中,是因为我不想打扰图书馆。但是,这确实很烦人,因为每次对程序进行更改时,都会丢失该程序的所有已保存数据/设置(假设您未使用 NSUserDefaults (用于首选项)。我一直不得不将其从旧版本转移到新版本。使用图书馆,您不必担心。

For a (possibly) more convincing reason of not to do this... When I was first learning Cocoa programming, I saved to the bundle because I didn't want to bother with the Library. It got really annoying, though, because every time you make a change to your program, you lose all of your saved data/settings for that program (assuming you're not using NSUserDefaults for preferences). I kept having to move it over from the old version to the new one. By using the Library, you don't have to worry about this.

这篇关于写入捆绑文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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