A file or file reference URL (as determined with fileURL), this property’s value is suitable for input into methods of NSFileManager or NSPathUtilities.
NSFileManager or NSPathUtilities
The NSFileManager class provides convenient access to a shared file manager object that is suitable for most types of file-related manipulations. A file manager object is typically your primary mode of interaction with the file system. You use it to locate, create, copy, and move files and directories.
Create the full file name
NSString* shortPath = [@"input" stringByAppendingPathExtension: @"txt"];
NSString* fullPath = [path stringByAppendingPathComponent: shortFileName];
// open the file. we convert to an NSURL so we can use the better open method
NSURL* fileURL = [NSURL fileURLWithPath: fullPath];
NSError* error = nil;
NSFileHandle *file=[NSFileHandle fileHandleForReadingFromURL: fileURL
error: &error];
if (file == nil)
{
// error contains info on what went wrong
}
else
{
// do what you need
}
+ (NSString *)pathWithComponents:(NSArray *)components;
- (NSArray *)pathComponents;
- (BOOL)isAbsolutePath;
- (NSString *)lastPathComponent; // frequently-used
- (NSString *)stringByDeletingLastPathComponent; // frequently-used
- (NSString *)stringByAppendingPathComponent:(NSString *)str; // frequently-used
- (NSString *)pathExtension; // frequently-used
- (NSString *)stringByDeletingPathExtension; // frequently-used
- (NSString *)stringByAppendingPathExtension:(NSString *)str; // frequently-used
- (NSString *)stringByAbbreviatingWithTildeInPath;
- (NSString *)stringByExpandingTildeInPath;
- (NSString *)stringByStandardizingPath;
- (NSString *)stringByResolvingSymlinksInPath;
- (NSArray *)stringsByAppendingPaths:(NSArray *)paths;
- (NSUInteger)completePathIntoString:(NSString **)outputName
caseSensitive:(BOOL)flag matchesIntoArray:(NSArray **)outputArray
filterTypes:(NSArray *)filterTypes;
- (__strong const char *)fileSystemRepresentation;
- (BOOL)getFileSystemRepresentation:(char *)cname maxLength:(NSUInteger)max;
Path
A path defines the location of a file in a file system.
/Users/John/myfile.txt
Paths can also become quite convoluted.
~demo/objc/code/../header/./mycode.h
Temporary Directory
Most operating systems have a standard directory provided specifically for the purposes of temporarily storing files.
it is much safer to use the NSTemporaryDirectory() to identify the appropriate directory. This function returns the temporary directory for the current user in the form of an NSString object.
the temporary directory using the NSTemporaryDirectory() function
NSString *tempdir;
tempdir = NSTemporaryDirectory();
NSLog (@"Temp Dir = %@", tempdir);
Getting the Home Directory
The home directory of the current user can be identified using the NSHomeDirectory() function. This function takes no arguments and returns an NSString object containing the path to the home directory of the user executing the program
NSString *homedir;
homedir = NSHomeDirectory();
NSLog (@"Home directory of current user is %@", homedir);
Getting the Home Directory of a Specified User
The home directory of any user on a system can be obtained using the NSHomeDirectoryForUser() function. This function takes as its sole argument an NSString object containing the name of the user and returns another NSString object containing the corresponding home directory
NSString *homedir;
NSString *username = @"Paul";
homedir = NSHomeDirectoryForUser(username);
NSLog (@"Home directory of user %@ is %@", username, homedir);
Extracting the Filename from a Path
A path can consist of the directory in which a file is located, followed by the name of the file. A common requirement when working with files when programming in any language is to extract just the file name from a path.
NSString *samplepath = @"/Users/demo/objc/sample.m";
NSString *filename;
filename = [samplepath lastPathComponent];
NSLog (@"lastPathComponent = %@", filename);
Extracting the Filename Extension
Filenames with characters after the final ‘.’ character are said to have a filename extension.
NSString *samplepath = @"/Users/demo/objc/sample.m";
NSString *pathext;
pathext = [samplepath pathExtension];
NSLog (@"pathExtension = %@", pathext);
Standardizing a Path
It is surprising how quickly a path can become complicated, especially when allowing a user to navigate through a file system. The NSUtilities package provides the stringByStandardizingPath method.
~demo/objc/code/../header/./../includes/mycode.h
NSString *samplepath = @"/Users/demo/objc/code/../header/./../includes/mycode.h";
NSString *cleanpath;
cleanpath = [samplepath stringByStandardizingPath];
NSLog (@"Standardized path = %@", cleanpath);
output
/Users/demo/objc/includes/mycode.h
iOS Standard Directories: Where Files Reside
For security purposes, an iOS app’s interactions with the file system are limited to the directories inside the app’s sandbox directory. During installation of a new app, the installer creates a number of container directories for the app inside the sandbox directory. Each container directory has a specific role. The bundle container directory holds the app’s bundle, whereas the data container directory holds data for both the app and the user. The data container directory is further divided into a number of subdirectories that the app can use to sort and organize its data. The app may also request access to additional container directories—for example, the iCloud container—at runtime.
Happy Coding 🙂
Discover more from CODE t!ps
Subscribe to get the latest posts sent to your email.