简体   繁体   English

我如何修复Cocoa错误513?

[英]How do I fix Cocoa error 513?

I have an iPhone application in which a number of domain objects are populated with user-entered data. 我有一个iPhone应用程序,其中许多域对象都填充了用户输入的数据。 In order to restore state after being interrupted, these objects implement the NSCoding protocol and are written to disk (the Documents directory) in the applicationWillTerminate message. 为了在中断后恢复状态,这些对象实现NSCoding协议并在applicationWillTerminate消息中写入磁盘(Documents目录)。 Then, when the application is launched again, the bytes of data are loaded up and the domain objects are repopulated. 然后,当再次启动应用程序时,将加载数据字节并重新填充域对象。 Code to get the documents directory is as follows: 获取文件目录的代码如下:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

This worked great in the simulator but once I deployed the app to the iPhone it stopped working. 这在模拟器中运行得很好,但是一旦我将应用程序部署到iPhone,它就停止了工作。 The reason why is iPhone error code 513 - which apparently means "permission denied." iPhone错误代码513的原因 - 显然意味着“许可被拒绝”。 There is a note in the iPhone dev docs that explain a little bit more - iPhone开发文档中有一个说明可以解释一下 -

On the device, the returned path (documentsDirectory) is similar to the following: /var/mobile/Applications/30B51836-D2DD-43AA-BCB4-9D4DADFED6A2/Documents 在设备上,返回的路径(documentsDirectory)类似于以下内容:/ var / mobile / Applications / 30B51836-D2DD-43AA-BCB4-9D4DADFED6A2 / Documents

However, on the Simulator, the returned path takes the following form: 但是,在模拟器上,返回的路径采用以下形式:

/Volumes/Stuff/Users/johnDoe/Library/Application Support/iPhone Simulator/User/Applications/118086A0-FAAF-4CD4-9A0F-CD5E8D287270/Documents / Volumes / Stuff / Users / johnDoe / Library / Application Support / iPhone Simulator / User / Applications / 118086A0-FAAF-4CD4-9A0F-CD5E8D287270 / Documents

This is the exact behavior that I'm seeing. 这是我所看到的确切行为。 I'm not really sure how this relates to getting a permission denied error and what I can do to fix it. 我不确定这是如何与获得权限被拒绝的错误以及我可以做些什么来解决它。 It does say below - 它确实如下 -

To read and write user preferences, use the NSUserDefaults class or the CFPreferences API. 要读取和写入用户首选项,请使用NSUserDefaults类或CFPreferences API。 These interfaces eliminate the need for you to construct a path to the Library/Preferences/ directory and read and write preference files directly. 这些接口使您无需构建Library / Preferences /目录的路径并直接读取和写入首选项文件。 For more information on using these interfaces, see “Adding the Settings Bundle.” 有关使用这些界面的更多信息,请参阅“添加设置包”。

If your application contains sound, image, or other resources in the application bundle, you should use the NSBundle class or CFBundle opaque type to load those resources. 如果应用程序包含应用程序包中的声音,图像或其他资源,则应使用NSBundle类或CFBundle opaque类型来加载这些资源。 Bundles have an inherent knowledge of where resources live inside the application. 捆绑包具有资源在应用程序内部的固有知识。 In addition, bundles are aware of the user's language preferences and are able to choose localized resources over default resources automatically. 此外,捆绑包可以识别用户的语言首选项,并且能够自动选择默认资源上的本地化资源。 For more information on bundles, see “The Application Bundle.” 有关bundle的更多信息,请参阅“应用程序包”。

I don't see how I can use the Application Bundle to load bytes of data though. 我不知道如何使用Application Bundle来加载数据字节。 Any help or examples? 有什么帮助或例子吗?

不确定这是如何工作的,但显然使用stringByAppendingPathComponent而不是stringByAppendingString来创建路径来解决问题。

The paragraph related to the application bundle refers to: 与应用程序包相关的段落指的是:

NSString *path = [[NSBundle mainBundle] pathForResource:@"somefile" ofType:@"png"];

This and other methods from NSBundle allow you to refer resources from inside the application bundle without actually knowing where the application bundle is located. NSBundle中的这个和其他方法允许您从应用程序包内部引用资源,而无需实际知道应用程序包的位置。

Disclaimer: I haven't worked with the iPhone. 免责声明:我没有使用iPhone。

With the disclaimer in mind, in plain OS X it's considered bad form to write stuff inside the application bundle. 考虑到免责声明,在简单的OS X中,在应用程序包中编写内容被认为是不好的形式。 You save stuff under the user's Application Support directory -> see NSApplicationSupportDirectory. 您可以在用户的应用程序支持目录下保存内容 - >请参阅NSApplicationSupportDirectory。

I got this error (513) because the path was wrong. 我收到此错误(513)因为路径错误。 Double checking my path fixed the problem :) 仔细检查我的路径修复了问题:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM