How to check if a folder is empty and create file names inside a folder in NSStrings? (iphone cocoa)

Just wondering, how can I check if a particular folder supports files, and create file names inside a folder in NSStrings? I know a class called NSFileManager, but I'm not sure how to apply it in accordance with my task.

+6
source share
2 answers

By default, all your user files and data will be saved in the document directory in your application. I have provided the sample code below to access the default document directory; plus a custom folder that you might call "MyFolderName" there

The end result is an array that has a list of NSString objects for files or directories in the path you specify.

//Accessing the default documents directory NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; //Appending the name of your custom folder, if you have any NSString *path = [documentsDirectory stringByAppendingPathComponent:@"MyFolderName"]; NSFileManager *fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:path]) { // Directory exists NSArray *listOfFiles = [fileManager contentsOfDirectoryAtPath:path error:nil]; } 

Hope this helps! :)

+5
source
 NSArray * files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderLocationString error:nil]; 
+6
source

Source: https://habr.com/ru/post/896837/


All Articles