Search code examples
iosxcodehome-directory

IOS: verify an empty NSHomeDirectory


can I verify if inside my folder "Documents" in NSHomeDirectory are there or not at least 1 file?

NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];

// Point to Document directory
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

// Write out the contents of home directory to console
NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);

Solution

  • Sure!

    NSFileManager's contentsOfDirectoryAtPath: error: method returns an array.

    If it's an array with a count of zero, it's an empty directory.

    If it returns an error, then the directory doesn't exist (or you passed in the path wrong).