0

I am trying to load a TableView with powerpoint files that are stored in the MainBundle of my app. I am having much difficulty.

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 app crashes and I get warnings on the last line stating incompatible pointer types sending NSArray to NSString. How can I best go about loading the PPT files into a TableView?

user717452
  • 33
  • 14
  • 73
  • 149
  • You are getting that warning because `self.files` is `NSString` whereas the result is in an array, try using `objectAtIndex:0`just before your retain. – iNoob Apr 09 '12 at 04:09
  • @iNoob That still is giving me the same error. `self.files = [[[[manager contentsOfDirectoryAtPath:paths error:nil] pathsMatchingExtensions:[NSArray arrayWithObjects:@"ppt", nil]] objectAtIndex:0] retain];` Error I get in console is `2012-04-08 23:15:58.329 AimTrue[53316:f803] -[__NSArrayM fileSystemRepresentation]: unrecognized selector sent to instance 0x687f810 2012-04-08 23:15:58.332 AimTrue[53316:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM fileSystemRepresentation]: unrecognized selector sent to instance` – user717452 Apr 09 '12 at 04:17
  • Can you just log your `NSArray *paths` ? Check if it is empty or not ? – iNoob Apr 09 '12 at 04:24
  • I'm guessing that's why its crashing, can you try with a single power point file first like: `NSString*myPPtFile=[[NSBundle mainBundle]pathForResource:@"yourPPTFile" ofType:@"ppt"];` ? – iNoob Apr 09 '12 at 04:29
  • @iNoob Ok, tried that and my log for paths returned the location and filename of the power point file. My code is now: `NSBundle *bundle = [NSBundle mainBundle]; NSString *paths = [bundle pathForResource:@"SongTitle" ofType:@"ppt"]; NSLog(@"%@", paths); NSFileManager *manager = [NSFileManager defaultManager]; self.title = @"Devo Songs"; self.files = [[[manager contentsOfDirectoryAtPath:paths error:nil] pathsMatchingExtensions:[NSArray arrayWithObjects:@"ppt", nil]] retain];` However, it still returns null for self.files count – user717452 Apr 09 '12 at 04:32
  • Yes the array will return null, i've no idea on how to return an array for path in the bundle, i usually use single files to display, you can try having all the file names that you've added in an array, and use `[objectAtIndex]` in `pathForResource:[yourArray objectAtIndex:0]`, i'm not sure how to get every filepath though. – iNoob Apr 09 '12 at 04:42
  • I Will try searching on how to get the list of every element in the bundle. – iNoob Apr 09 '12 at 04:43
  • Ok found one solution from here http://stackoverflow.com/questions/418680/subdirectories-within-an-ios-application, `pathForResourceOfType` works only on folders not groups, so the `inDirectory` should be a folder (which will be in blue color not yellow), so make sure the list of songs is in a folder for `pathForResourceOfType` to work not in a group. – iNoob Apr 09 '12 at 04:58
  • This worked to finally get it...thanks for the link to adding folder and not group. ` NSBundle *bundle = [NSBundle mainBundle]; NSArray *paths = [bundle pathsForResourcesOfType:@"ppt" inDirectory:@"powerpoint"]; NSLog(@"%@", paths); self.title = @"Devo Songs"; self.files = paths;` The issue I now get is the cell.text displays the entire path of the .ppt and not just the file name. What would be the best way to return that? – user717452 Apr 09 '12 at 13:24
  • I know I could use `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. – user717452 Apr 09 '12 at 17:04
  • You need to create a `NSMutableArray` object, and keep adding each `NSString` into that array, say if `paths` has 10 elements, you can do `for(int i=0;i<[paths count];i++) {NSString*someFileName=[paths objectAtIndex:i]; [yourMutableArray addObject:someFileName]; } then you show this mutable array in your table's `cellForRowAtIndexPath` method. That will show the 10 elements of `paths` array – iNoob Apr 10 '12 at 03:46
  • Saw that you already got an answer :D, good luck. – iNoob Apr 10 '12 at 03:48

0 Answers0