简体   繁体   English

需要有关NSFileManager上创建目录方法的帮助

[英]Need assistance regarding create directory methods on NSFileManager

NSFileManager has two methods to create directory, one ask for path and another ask for url . NSFileManager有两种创建目录的方法,一种是询问path ,另一种是请求url What url is for? 什么url是为了什么? Urls are for network but I don't see any network stuff in doc. 网址是网络的,但我在doc中看不到任何网络内容。

createDirectoryAtPath:withIntermediateDirectories:attributes:error:
createDirectoryAtURL:withIntermediateDirectories:attributes:error:

The answer is contained at the same link : 答案包含在同一链接中

The NSFileManager class supports both the NSURL and NSString classes as ways to specify the location of a file or directory. NSFileManager类支持NSURL和NSString类,作为指定文件或目录位置的方法。 The use of the NSURL class is generally preferred for specifying file-system items because they can convert path information to a more efficient representation internally. 通常首选使用NSURL类来指定文件系统项,因为它们可以在内部将路径信息转换为更有效的表示。 You can also obtain a bookmark from an NSURL object, which is similar to an alias and offers a more sure way of locating the file or directory later. 您还可以从NSURL对象获取书签,该书签与别名类似,并提供更有把握的方法来稍后查找文件或目录。

URLs come in 2 forms, network and file . URL有两种形式,网络和文件 See: 看到:

NSURL *filePathURL = [NSURL fileURLWithPath:...];

A URL is just an organised definition of a path when you boil it down. 当您将其煮沸时,URL只是路径的有组织定义。

To add to Visput and Wain's answers. 添加到Visput和Wain的答案。

In your code you may be returning a file or directory path that is in the form of a NSURL from another method 在您的代码中,您可能会从另一个方法返回NSURL形式的文件或目录路径

An example would be the returned selection of a NSOpenPanel . 一个例子是返回的NSOpenPanel选择。

It is alos easier to access the parts that make up the path in an URL than it is in a string path 在URL中访问构成路径的部分比在字符串路径中更容易

NSFileManager gives you the convenience method to use a NSURL that you already have from some where else. NSFileManager为您提供了一种方便的方法来使用您已经拥有的NSURL。

Read up on NSURL 阅读NSURL

Overview 概观

An NSURL object represents a URL that can potentially contain the location of a resource on a remote server, the path of a local file on disk, or even an arbitrary piece of encoded data. NSURL对象表示可能包含远程服务器上资源位置的URL,磁盘上本地文件的路径,甚至是任意一段编码数据。

You can use URL objects to construct URLs and access their parts. 您可以使用URL对象构建URL并访问其部件。 For URLs that represent local files, you can also manipulate properties of those files directly, such as changing the file's last modification date. 对于表示本地文件的URL,您还可以直接操作这些文件的属性,例如更改文件的上次修改日期。 Finally, you can pass URL objects to other APIs to retrieve the contents of those URLs. 最后,您可以将URL对象传递给其他API以检索这些URL的内容。 For example, you can use the NSURLSession, NSURLConnection, and NSURLDownload classes to access the contents of remote resources, as described in URL Loading System Programming Guide. 例如,您可以使用NSURLSession,NSURLConnection和NSURLDownload类来访问远程资源的内容,如URL加载系统编程指南中所述。

URL objects are the preferred way to refer to local files. URL对象是引用本地文件的首选方式。 Most AppKit objects that read data from or write data to a file have methods that accept an NSURL object instead of a pathname as the file reference. 从文件读取数据或将数据写入文件的大多数AppKit对象都具有接受NSURL对象而不是路径名作为文件引用的方法。 For example, you can get the contents of a local file URL as an NSString object by calling the stringWithContentsOfURL:encoding:error: method, or as an NSData object by calling the dataWithContentsOfURL:options:error: method. 例如,您可以通过调用stringWithContentsOfURL:encoding:error:方法获取本地文件URL的内容作为NSString对象,或通过调用dataWithContentsOfURL:options:error:方法获取NSData对象。

You can also use URLs for interapplication communication. 您还可以使用URL进行应用程序间通信。 In OS X, the NSWorkspace class provides the openURL: method to open a location specified by a URL. 在OS X中,NSWorkspace类提供openURL:方法来打开URL指定的位置。 Similarly, in iOS, the UIApplication class provides the openURL: method. 同样,在iOS中,UIApplication类提供了openURL:方法。

Additionally, you can use URLs when working with pasteboards, as described in NSURL Additions Reference (part of the AppKit framework). 此外,您可以在使用粘贴板时使用URL,如NSURL Additions Reference(AppKit框架的一部分)中所述。

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

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