I'm having some trouble printing a list of file names retrieved with NSFileManager. The documentation says it returns an array of strings - but my output is blank. In the code sample below, execution reveals the index with colon and nothing else... I am a long time java programmer but total newb to c/objc. Am I just misusing printf? Thanks for your help!
Output:
About to print file list -----------------------
Number of files: 29
0: 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28:
- (IBAction)printFileList:(id)sender{
NSFileManager *fm = [[NSFileManager alloc] init];
NSString *path = @"/";
NSArray *files = [fm contentsOfDirectoryAtPath:path error:NULL];
printf("About to print file list ----------------------- \n");
int fileCount = [files count];
printf("Number of files: %d", fileCount);
for(int i = 0; i<fileCount; i++){
printf("%d: %s\n", i, [files objectAtIndex:i]);
}
}
You can't use %s
as a format specifier for NSString *
, since NSString *
is a pointer, not a C string. Change your printf
line to:
printf("%d: %s\n", i, [[files objectAtIndex:i] UTF8String]);