Chapter 16
3rd paragraph in "Managing Files and Directories: NSFileManager" :
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):
#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 :
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.)