Amazon.com Widgets NSFileManager handles special character "." and ".." in pathnames
Welcome, Guest. Please login or register.
Did you miss your activation email?
July 30, 2014, 10:21:27 PM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Programming in Objective-C, 4th edition
| |-+  Chapter 16
| | |-+  NSFileManager handles special character "." and ".." in pathnames
Pages: [1]   Go Down
Print
Author Topic: NSFileManager handles special character "." and ".." in pathnames  (Read 786 times)
starfall
Newbie
*
Posts: 3


Email




« on: April 07, 2012, 05:36:05 AM »

Chapter 16
3rd paragraph in "Managing Files and Directories: NSFileManager" :

Quote
The special tilde character (~) is used as an abbreviation for a user’s home directory. ~linda would, therefore, be an abbreviation for the user linda’s home directory, which might be the path /Users/linda. A solitary tilde character indicates the current user’s home directory, meaning the pathname ~/copy1.m would reference the file copy1.m stored in the current user’s home directory. Other special UNIX-style pathname characters, such as . for the current directory and .. for the parent directory, should be removed from pathnames before they’re used by any of the Foundation file-handling methods. An assortment of path utilities are available that you can use for this, and they’re discussed later in this chapter.

I think this paragraph means that you can't use  . for the current directory and .. for the parent directory, in NSFileManager class's methods.

But after a few testing, I found that we can use both of them.

My test code (Mac OS X 10.7.3, Xcode 4.3.2):

Code: (Objective-C)
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        NSFileManager   *fm;
        NSString        *current, *parent, *relative;
        NSArray         *dirContent;
        int             i;
       
        fm = [NSFileManager defaultManager];
        current = [fm currentDirectoryPath];
        relative = @"../..";
        parent = [current stringByAppendingPathComponent:relative];
       
        NSLog(@"Current directory: %@", current);
        NSLog(@"Parent directory: %@", parent);
        NSLog(@"Relative directory: %@", relative);
       
        NSLog(@"Contents in parent:");
        dirContent = [fm contentsOfDirectoryAtPath:parent error:NULL];
        i = 0;
        for (NSString *path in dirContent) {
            NSLog(@"%d: %@", i, path);
            ++i;
        }
       
        NSLog(@"Contents in %@:", relative);
        dirContent = [fm contentsOfDirectoryAtPath:relative error:NULL];
        i = 0;
        for (NSString *path in dirContent) {
            NSLog(@"%d: %@", i, path);
            ++i;
        }
       
        NSLog(@"Contents in %@:", @".");
        dirContent = [fm contentsOfDirectoryAtPath:@"." error:NULL];
        i = 0;
        for (NSString *path in dirContent) {
            NSLog(@"%d: %@", i, path);
            ++i;
        }
    }
    return 0;
}

Result is :

Code: (Objective-C)
2012-04-07 20:45:56.578 ttt[81284:403] Current directory: /Users/Jackey/Library/Developer/Xcode/DerivedData/ttt-behrcnxvtcyoughcpasnbynamrth/Build/Products/Debug
2012-04-07 20:45:56.580 ttt[81284:403] Parent directory: /Users/Jackey/Library/Developer/Xcode/DerivedData/ttt-behrcnxvtcyoughcpasnbynamrth/Build/Products/Debug/../..
2012-04-07 20:45:56.582 ttt[81284:403] Relative directory: ../..
2012-04-07 20:45:56.583 ttt[81284:403] Contents in parent:
2012-04-07 20:45:56.587 ttt[81284:403] 0: Intermediates
2012-04-07 20:45:56.588 ttt[81284:403] 1: Products
2012-04-07 20:45:56.589 ttt[81284:403] Contents in ../..:
2012-04-07 20:45:56.590 ttt[81284:403] 0: Intermediates
2012-04-07 20:45:56.590 ttt[81284:403] 1: Products
2012-04-07 20:45:56.591 ttt[81284:403] Contents in .:
2012-04-07 20:45:56.592 ttt[81284:403] 0: ttt

which shows those 2 symbols work in contentsOfDirectoryAtPath:error: method. (I replaced my user name with XXXX.)

« Last Edit: April 07, 2012, 05:48:09 AM by starfall » Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.