Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

newtoiphonesdk

macrumors 6502a
Original poster
Jul 30, 2010
567
2
I have several ppt slides included with the app I am currently working on. I would like for the TableView to list all of those ppt files, but am running into an issue. The NSArray always stays empty. Here is what I have in the viewDidLoad
Code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSUserDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectoryPath = [paths objectAtIndex:0];
	NSFileManager *manager = [NSFileManager defaultManager];
	self.title = @"Songs";
	self.files = [[[manager contentsOfDirectoryAtPath:documentsDirectoryPath error:nil] pathsMatchingExtensions:[NSArray arrayWithObjects:@"ppt", nil]] retain];
	
	
	
	self.navigationItem.rightBarButtonItem = self.editButtonItem;
	[self.tableView reloadData];
    [super viewDidLoad];
    [super viewWillAppear:animated];
If, NSUserDirectory is not where files are stored that the developer adds in, what is?
 
Search for NSSearchPathForDirectoriesInDomains in this forum.

https://forums.macrumors.com/search/?searchid=27277519

Yes, I have searched that. If you know the answer for how to return an Array of Files that I include in the mainBundle, then please answer the question. Don't waste time making a post that tells me to search, when I have already searched. "O GEE GOLLY! I can search on this here internet thingy?! I Never knew that...thank you for the useful post!"
 
Perhaps [NSBundle mainBundle], possibly modified by pathForResource:xx:?

I honestly can't figure out what answer you're looking for.

First, the code uses NSUserDirectory, but has no comments or other explanation about why that's the correct directory. And since it's not working, apparently it isn't the correct directory.

Second, the code has a variable named documentsDirectoryPath, which made me think "Maybe you want the documents directory", rather than NSUserDirectory.

Third, you write "where files are stored that the developer adds in", which seems to be a description of what you want, but it's too brief and ambiguous for me to know exactly how to answer. It's ambiguous because you don't describe what you mean by "the developer adds in". Do you mean "adds to the app after it's installed"? That's probably the documents directory. Or do you mean "adds to the project, and copies to the main bundle at compile-time", which would be the main bundle directory.

Finally, you eventually mention mainBundle, so that's probably where you should start looking. But I'm still guessing, because I still don't know exactly what you're looking for.

If you don't know where files that are part of your build process are being placed in the main bundle, you should stop and look in your built app's main bundle for where those files are being placed. If you don't see them anywhere, then maybe they aren't being copied in the build, and that's why they don't appear.
 
Only a guess, but try this instead.

Code:
NSArray *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

Also you can get the app's sandbox path like this.

Code:
NSString *sandboxPath = NSHomeDirectory();

// and append a document directory off the home directory

NSString *documentPath = [sandboxPath stringByAppendingPathComponent:@"Documents"];

But this second way is not recommended, as Apple might change the name of the Documents folder in later releases of the operating system.

Hope this is of some help.

Regards Mark
 
Perhaps [NSBundle mainBundle], possibly modified by pathForResource:xx:?

I honestly can't figure out what answer you're looking for.

First, the code uses NSUserDirectory, but has no comments or other explanation about why that's the correct directory. And since it's not working, apparently it isn't the correct directory.

Second, the code has a variable named documentsDirectoryPath, which made me think "Maybe you want the documents directory", rather than NSUserDirectory.

Third, you write "where files are stored that the developer adds in", which seems to be a description of what you want, but it's too brief and ambiguous for me to know exactly how to answer. It's ambiguous because you don't describe what you mean by "the developer adds in". Do you mean "adds to the app after it's installed"? That's probably the documents directory. Or do you mean "adds to the project, and copies to the main bundle at compile-time", which would be the main bundle directory.

Finally, you eventually mention mainBundle, so that's probably where you should start looking. But I'm still guessing, because I still don't know exactly what you're looking for.

If you don't know where files that are part of your build process are being placed in the main bundle, you should stop and look in your built app's main bundle for where those files are being placed. If you don't see them anywhere, then maybe they aren't being copied in the build, and that's why they don't appear.

I have a group "Resources" in my MainBundle. In that group, I store 100 different powerpoint files. I want the TableView to display those. All I need is to get the array set to return the ppt files in Resources.
 
You should search all the files on the main bundle. If you can't you can always copy all of the ppt files to the document directory on startup.
 
You should search all the files on the main bundle. If you can't you can always copy all of the ppt files to the document directory on startup.

Here is what I'm trying now, where self.files is an NSArray
Code:
  NSBundle *bundle = [NSBundle mainBundle];
   NSArray *paths  = [bundle pathsForResourcesOfType:@"ppt" inDirectory:@"Resources"];
	NSFileManager *manager = [NSFileManager defaultManager];
	self.title = @"Devo Songs";
	self.files = [[[manager contentsOfDirectoryAtPath:paths error:nil] pathsMatchingExtensions:[NSArray arrayWithObjects:@"ppt", nil]] retain];
The issue is with that last line of code, Incompatible pointer types sending NSArray to parameter of type NSString.

I'm not for sure how to get this to all work together. I've loaded TableViews form documents before, but it has always been from NSUserDocument stuff.
 
I have finally got it to add the files to the Table View using:
Code:
 NSBundle *bundle = [NSBundle mainBundle];
    
    NSArray *paths  = [bundle pathsForResourcesOfType:@"ppt" inDirectory:@"powerpoint"];
    NSLog(@"%@", paths);
	self.title = @"Devo Songs";
	self.files = paths;
The issue I now face is with the cell text. I use:
Code:
	cell.textLabel.text = [self.files objectAtIndex:indexPath.row];
But this is giving me the entire path of /Users/name/XCODE/Applications/.../.../song.ppt
What would be the better way to get ONLY the file name, without the entire path?

UPDATE:
I know I could use
Code:
        NSString *documentsDirectoryPath = [paths objectAtIndex:0];

self.filenames = [[documentsDirectoryPath lastPathComponent] stringByDeletingPathExtension];
But, that would only give me the first item in the TableView, and am having a hard time figuring out getting NSString to NSArray and everything. Array can't do lastPathComponent, and NSString won't respond to indexPath.row, so I'm a bit lost.
 
Last edited:
I have finally got it to add the files to the Table View using:
Code:
 NSBundle *bundle = [NSBundle mainBundle];
    
    NSArray *paths  = [bundle pathsForResourcesOfType:@"ppt" inDirectory:@"powerpoint"];
    NSLog(@"%@", paths);
	self.title = @"Devo Songs";
	self.files = paths;
The issue I now face is with the cell text. I use:
Code:
	cell.textLabel.text = [self.files objectAtIndex:indexPath.row];
But this is giving me the entire path of /Users/name/XCODE/Applications/.../.../song.ppt
What would be the better way to get ONLY the file name, without the entire path?
Break it down. Think it through.

You seem to be flailing, trying random things that might lead to a solution. Instead of that, try thinking about exactly what a solution would look like. Then work backwards from that, step by step.

You've already stated what the solution will look like: only the file name, without the entire path, stored in an array of file names. So what is a class that has the ability to scan a directory, returning an array that contains the pathnames? You already used it in an earlier post.

Next, given a full pathname in an NSString, how do you break it down into pathname components? Or maybe just return the last path component? So now iterate over the array of full pathnames, replacing each full pathname with its last path component. Or just make a new array containing only the last path components. You now have an array of just file names.

Next step back, how did you tell the scanning class which directory to scan? You give it the path of the directory. Not the path of the files in the directory, but the path of the directory itself. So now think about how to get the path of a directory located in the main bundle. Again, you've already used the class, you just need to find the right method.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.