简体   繁体   English

获取writeToFile:原子地:保存到的URL

[英]Getting writeToFile:atomically: URL saved to

I'm saving some NSData with writeToFile:atomically: and then want to use that file very shortly afterwards. 我正在使用writeToFile:atomically:保存一些NSData writeToFile:atomically:然后很快就想要使用该文件。 What is the easiest way to get the NSURL of the file I just saved? 获取我刚刚保存的文件的NSURL的最简单方法是什么?

[NSData writeToFile: atomically:] has a path parameter. [NSData writeToFile: atomically:]有一个path参数。

Just make the path parameter into a file URL and save that as an instance variable or property and you'll be all set. 只需将path参数放入文件URL并将其保存为实例变量或属性即可。

Or even better, use [NSData writeToURL: atomically:] (ie convert your path into a URL to start with) and save that as a property or instance variable to be used later on. 甚至更好,使用[NSData writeToURL: atomically:] (即转换您的路径进入一个网址即可开始使用),并保存为一个属性或实例变量将在后面使用。

You can convert a path into a file URL via [[NSURL alloc] initFileURLWithPath:] . 您可以通过[[NSURL alloc] initFileURLWithPath:]将路径转换为文件URL。

And lastly, the real answer to the question you're trying to ask is: no, you can not divine the path or URL from an arbitrary " NSData " object. 最后,你要问的问题的真正答案是:不,你不能从任意“ NSData ”对象中NSData路径或URL。 You'll have to save that information separately or alongside your data in order to keep track of where it came from. 您必须单独或与数据一起保存该信息,以便跟踪它的来源。

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

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