Question

NSFileManager has two methods to create directory, one ask for path and another ask for url. What url is for? Urls are for network but I don't see any network stuff in doc.

createDirectoryAtPath:withIntermediateDirectories:attributes:error:
createDirectoryAtURL:withIntermediateDirectories:attributes:error:
Was it helpful?

Solution

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. 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. 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.

OTHER TIPS

URLs come in 2 forms, network and file. See:

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

A URL is just an organised definition of a path when you boil it down.

To add to Visput and Wain's answers.

In your code you may be returning a file or directory path that is in the form of a NSURL from another method

An example would be the returned selection of a NSOpenPanel.

It is alos easier to access the parts that make up the path in an URL than it is in a string path

NSFileManager gives you the convenience method to use a NSURL that you already have from some where else.

Read up on 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.

You can use URL objects to construct URLs and access their parts. For URLs that represent local files, you can also manipulate properties of those files directly, such as changing the file’s last modification date. Finally, you can pass URL objects to other APIs to retrieve the contents of those URLs. 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.

URL objects are the preferred way to refer to local files. 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. 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.

You can also use URLs for interapplication communication. In OS X, the NSWorkspace class provides the openURL: method to open a location specified by a URL. Similarly, in iOS, the UIApplication class provides the openURL: method.

Additionally, you can use URLs when working with pasteboards, as described in NSURL Additions Reference (part of the AppKit framework).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top