On Nov 23, 2008, at 11:56 AM, Kevin Gessner wrote:
Rather than hitting the file system every time you want to check file existence, you could cache it from -[NSFileManager directoryContentsAtPath:]. You could run in to a race condition (if another process creates a file between when you check the list and when you write your file), but that's true of your iterative solution as well.
This is a good idea, but you do need to be careful. For instance, the following program reports this:
froude:tmp amaxwell$ cc -o nametest -framework Foundation nametest.m froude:tmp amaxwell$ ./nametest 2008-11-23 12:32:40.408 nametest[2186:10b] created Test File2008-11-23 12:32:40.411 nametest[2186:10b] contentsOfDirectoryAtPath contains Test File 2008-11-23 12:32:40.412 nametest[2186:10b] fileExistsAtPath: says that Test File exists
2008-11-23 12:32:40.412 nametest[2186:10b] created Test file2008-11-23 12:32:40.413 nametest[2186:10b] contentsOfDirectoryAtPath does not contain Test file 2008-11-23 12:32:40.413 nametest[2186:10b] fileExistsAtPath: says that Test file exists
froude:tmp amaxwell$ ll Test\ File -rw-r--r-- 1 amaxwell wheel 0 Nov 23 12:32 Test File"Test File" exists, but checking contentsOfDirectoryAtPath: and fileExistsAtPath: for the existence of "Test file" gives contradictory results.
In some respects, it would be nice if the array returned by NSFileManager used case-insensitive equality callbacks when called on a case-insensitive filesystem. Cocoa has a number of weird edge cases like this (e.g. instances of NSURL for files may not compare equal for the same file: URL, depending on how they were created).
-- Adam #import <Foundation/Foundation.h> int main (int argc, char const *argv[]) { NSAutoreleasePool *pool = [NSAutoreleasePool new]; NSFileManager *fm = [NSFileManager defaultManager]; NSString *fname = @"Test File";if ([fm createFileAtPath:fname contents:[NSData data] attributes:nil])
NSLog(@"created %@", fname);NSArray *content = [fm contentsOfDirectoryAtPath:[fm currentDirectoryPath] error:NULL];
if ([content containsObject:fname]) NSLog(@"contentsOfDirectoryAtPath contains %@", fname); else NSLog(@"contentsOfDirectoryAtPath does not contain %@", fname); if ([fm fileExistsAtPath:fname]) NSLog(@"fileExistsAtPath: says that %@ exists", fname); else NSLog(@"fileExistsAtPath: says that %@ does not exist", fname); fname = @"Test file";if ([fm createFileAtPath:fname contents:[NSData data] attributes:nil])
NSLog(@"created %@", fname); if ([content containsObject:fname]) NSLog(@"contentsOfDirectoryAtPath contains %@", fname); else NSLog(@"contentsOfDirectoryAtPath does not contain %@", fname); if ([fm fileExistsAtPath:fname]) NSLog(@"fileExistsAtPath: says that %@ exists", fname); else NSLog(@"fileExistsAtPath: says that %@ does not exist", fname); [pool release]; return 0; }
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]