• Home
  • What is ?
  • DSA
  • MAD
  • Concept
  • Practice
  • Misc
  • Quiz
  • YT
  • Home
  • What is ?
  • DSA
  • MAD
  • Concept
  • Practice
  • Misc
  • Quiz
  • YT
  • #News
  • #APPS
  • #Events
    • #WWDC
    • #I/O
    • #Ignite
  • #Let’s Talk
  • #Interview
  • #Tips

MyCodeTips mycodetips-newlogocopy1

  • Home
  • What is ?
  • DSA
  • MAD
  • Concept
  • Practice
  • Misc
  • Quiz
  • YT
Objective-c

NSFileManager or NSPathUtilities in Objective-C

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 🙂

Liked it? Take a second to support Ranjan on Patreon!
Become a patron at Patreon!
  • Click to share on Reddit (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • More
  • Click to share on Pocket (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
Written by Ranjan - 5962 Views
Tags | IOS, nspath, objective-c
AUTHOR
Ranjan

Namaste, My name is Ranjan, I am a graduate from NIT Rourkela. This website is basically about of what i learnt from my years of experience as a software engineer on software development specifically on mobile application development, design patterns/architectures, its changing scenarios, security, troubleshooting, tools, tips&tricks and many more.

You Might Also Like

iOS Logo

Tips to consume .net webservice in IOS applications

March 5, 2014
mycodetips-newlogo2

Tips to Loading image from a URL to UIImage object

March 10, 2014
FloatingPointNumbers

What is Floating-Point Operations

September 2, 2019
Next Post
Previous Post

Support us

mycodetips
mycodetips

Follow us @ LinkedIn 2850+

Subscribe for updates

Join 8,213 other subscribers

Latest Posts

  • YT-Featured-solidprinciples
    SOLID Principles of Software Design
  • IOS 16 Features
    Latest features in IOS 16
  • r-language
    How can R language be used for data analysis?
  • wordpress-coding-blog
    Guide To WordPress Coding Standards
  • YT-Featured-Algorithm
    What is Algorithm?
whiteboard

Whiteboard(PRO)

whiteboard

Whiteboard(lite)

alphabets

Kids Alphabet

techlynk

Techlynk

techbyte

Do2Day

techbyte

Techbyte

  • #about
  • #myapps
  • #contact
  • #privacy
  • #Advertise
  • #myQuestions

Android Android Studio API APP Programming Apps blogging CSS DATABASE dsa Features HTML HTML5 installation Interview Questions IOS iPhone javascript Mac objective-c OS Programming quicktips SDK SEO SQL swift Tips & Tricks Tools UI Web Wordpress Xcode

  • SOLID Principles of Software Design
  • Latest features in IOS 16
  • How can R language be used for data analysis?
  • Guide To WordPress Coding Standards
  • What is Algorithm?

©mycodetips.com