Cocoa - NSFileManager removeItemAtPath 不工作
我正在尝试删除一个文件,但不知何故 nsfilemanager 不允许我这样做。我确实在一行代码中使用了该文件,但是一旦运行该操作,我希望删除该文件。我已经记录了错误代码和消息,并收到错误代码:4 和消息:
"text.txt" could not be removed
有没有办法“干净”地修复此错误(无需任何黑客攻击),以便苹果在其 Mac App Store 上接受此应用程序?
编辑:
这就是我正在使用的:
[[NSFileManager defaultManager] removeItemAtPath:filePath error:NULL];
谢谢,
凯文
I am trying to delete a file, but somehow nsfilemanager will not allow me to do so. I do use the file in one line of code, but once that action has been ran, I want the file deleted. I have logged the error code and message and I get error code: 4 and the message:
"text.txt" could not be removed
Is there a way to fix this error "cleanly" (without any hacks) so that apple will accept this app onto their Mac App Store?
EDIT:
This is what I am using:
[[NSFileManager defaultManager] removeItemAtPath:filePath error:NULL];
Thanks,
kevin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
错误代码 4 似乎是 NSNoSuchFileError。如果你要删除的文件确实存在,那么你的路径就错了。如果您希望我们准确地告诉您路径错误的原因,您需要发布一些代码。
如果该文件不存在,您可以忽略该错误。
Error code 4 seems to be NSNoSuchFileError. If the file you want to delete really exists, then you have got the path wrong. You'll need to post some code if you want us to tell you exactly how you got the path wrong.
If the file doesn't exist, you can ignore the error.
我在 swift 中遇到了类似的问题。由于某种原因 fileManager.removeItemAtPath 不起作用,我将 fileManager.removeItemAtPath(filePath) 更改为 fileManager.removeItemAtURL(fileURL) 并且它工作正常。
I had a similar problem in swift.For some reason fileManager.removeItemAtPath did't work and I changed fileManager.removeItemAtPath(filePath) to fileManager.removeItemAtURL(fileURL) and it works fine.
首先,您需要选择文档目录的路径,然后才能删除该文件。
仅删除语句是不够的。
用它来解决你的问题。
First you need to pick the path for the document directory then you can delete the file.
Only remove statement is not sufficient.
use this for solving your problem.
您的路径不正确
使用以下内容
而不是
方法“removeItemAtPath:”需要文件的本地路径,如果您想使用 url 删除,则应使用
-removeItemAtURL:
Your path is incorrect
Use the following
instead of
The method "removeItemAtPath:" need the local path of file, If you want to remove using url, you should use
-removeItemAtURL:
我只是从使用 NSFileManager 时非常重要的事情中发现了这一点。您必须了解应用程序沙箱。
此行返回沙盒应用程序的路径文档目录。当您在文档目录中使用 FileManager 创建文件时(例如),不要保存完整的文件路径,而仅保存当前文档目录的路径。
您将能够重新创建所创建文件的完整路径。
希望(5年后)能够帮助开发人员;-)
I just figure it out of something very important when you use NSFileManager. You have to be aware of App Sandboxing.
This line return the path document directory of your app sandboxed. When you create a file with FileManager in your document directory (for e.g) don't save the full file path but only the path from the current document directory.
You'll be able to recreate the full path of your created file.
Hope (after 5 years) help over developers ;-)
默认方法 AFNetworking 3.0 不会刷新您的下载器文件。
如果你想在 Objective-C +iOS9.0 中重写这个文件,你需要这样做:
Default method AFNetworking 3.0 don't refresh that you downloader files.
If you want rewrite this file in Objective-C +iOS9.0, you need do that: