开发者

How to check if folder is empty, and instantiate file names inside the folder into NSStrings? (iphone cocoa)

Just wondering, how would I check if a particular folder is holding files, and instantiate file names inside the f开发者_如何转开发older into NSStrings? I know of a class called NSFileManager, but I'm not sure how to apply it to suit my objective.


NSArray * files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderLocationString error:nil];


By default all your custom files and data will be stored in the documents directory in your app. I've put a sample code below to access the default document directory; plus a custom folder you may have in there called 'MyFolderName'

The end result will be an array which has a list of NSString objects of the files or directories in the path you have specified.

//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! :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜