ok i’ve been going round in circles for 1.5 hours, now i need some help. below is the code i’m using to create a list of files in my Documents Directory, all works fine unless the directory is empty.
fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error];
what can i do directly before this to check if the directory has contents?
ive tried
if ([[[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error] objectAtIndex:0]){
fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error];
}
but that also crashes the app
/////////////////////NOOB MISTAKE ALERT////////////////////
ok my problem was my next line of code
NSLog(@"fileList: %@",[fileList objectAtIndex:fileList.count-1]);
that’s what was crashing my app, my own fault i admit lol. [fileList objectAtIndex:fileList.count-1] is out of bounds, i’m confused as to why the debugger didn’t tell me that much.
Thanks for your help guys
The method should not crash even if there is no files. According to the doc,
I would check
documentsDirectoryvariable is properly set as a NSString instancedocumentsDirectoryvariable is not deallocated alreadyNSLogging
documentsDirectoryjust before the call would be the right first step.