Getting Contents of Directory

Discussion in 'iOS Programming' started by duny, Sep 29, 2008.

  1. duny macrumors newbie

    duny

    Joined:
    Sep 21, 2008
    Location:
    H-Town
    #1
    Can someone show me the code for the
    .h and .m ViewControl to obtain the files in a directory.
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    No need for a complete class to do this. Look at NSFileManager.
     
  3. GregInAZ macrumors newbie

    Joined:
    Aug 2, 2008
    #3
    Look at directoryContentsAtPath, which is part of NSFileManager. This will return an array.
     
  4. duny thread starter macrumors newbie

    duny

    Joined:
    Sep 21, 2008
    Location:
    H-Town
    #4
    K I read it as always, but i am still lost ing the sause, i read the NSDirectoryEnumerator, it's exactly what i am looking for, I follow the text but nothing, it says i need NSFileManager.h would that be the same as i ManagerViewController.h i have created. I hav a Table View Set up in one of my .XIB files, i want to be able to view the directory.

    No matter what i play with i still get errors. Can some one just show me barney style how this is done.
     
  5. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    Code:
    	NSDirectoryEnumerator*	e = [[NSFileManager defaultManager] enumeratorAtPath:MMDocumentsFolder()];
    	
    	// Ignore any files except XYZ.jpg
    	for (NSString*	file in e)
    	{
    		if (NSOrderedSame == [[file pathExtension] caseInsensitiveCompare:@"jpg"])
    		{
    			// Do something with file.jpg
    		}
    		else
    		if ([[[e fileAttributes] fileType] isEqualToString:NSFileTypeDirectory])
    		{
    			// Ignore any subdirectories
    			[e skipDescendents];
    		}
    	}
    
     
  6. duny thread starter macrumors newbie

    duny

    Joined:
    Sep 21, 2008
    Location:
    H-Town
  7. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #7
    It's source code. It goes in the method that you're writing that wants to scan a directory. Nothing needs to go in the .h file.
     
  8. duny thread starter macrumors newbie

    duny

    Joined:
    Sep 21, 2008
    Location:
    H-Town
    #8
    Here is what i got.

    untitled.m
    Code:
    - (void)viewWillAppear{
    	NSString *file;
    	NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:  @"Documents"];
    	NSDirectoryEnumerator *dirEnum =
        [[NSFileManager defaultManager] enumeratorAtPath:docsDir];
    	
    	while (file = [dirEnum nextObject]) {
    		if ([[file pathExtension] isEqualToString: @"doc"]) {
    			[self scanDocument: [docsDir stringByAppendingPathComponent:file]];
    		}
    	}
    }
    @end
    

    And Untitled.h
    Code:
    @interface ManageViewController : UITableViewController {
    		
    }
    
    @end
    
    The error i get when i compile is
    warning "ManageViewCpntroller" may not reposnd to '-scanDocument'
     
  9. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #9
    Of course it may not respond. You didn't implement any such method.

    Let me suggest that you spend some time doing tutorials. Perhaps buy a Cocoa book like the Hillegas book and run through the examples in that book or an iPhone dev book when they become available.
     
  10. duny thread starter macrumors newbie

    duny

    Joined:
    Sep 21, 2008
    Location:
    H-Town
    #10
    Cant you just help me out. The iphone dev book doesn't come out till January.
     
  11. robotspacer macrumors member

    Joined:
    Jun 25, 2007
    #11
    You're calling a method that you haven't created yet, so helping you out would basically amount to writing your app for you!

    I think you definitely need to spend some time learning the basics of Objective-C and Cocoa. The aforementioned book by Aaron Hillegass is a great place to start: http://www.amazon.com/Cocoa-Programming-Mac-OS-3rd/dp/0321503619/

    So is Cocoa Dev Central:
    http://cocoadevcentral.com

    Both of these deal with specifically Mac software development, so not everything will apply, but the basics (and even some of the more complex stuff) are very very similar.

    I also highly recommend looking at the various sample projects Apple has posted at http://developer.apple.com
     
  12. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #12
    Hillegass's new book is already out and will help you understand some of the basic concepts you are missing. Maybe a C primer if that's over your head. Also, read as much of the Cocoa ond Objective C documentation on the Apple site as you can. Otherwise it's too much like explaining how to cook fettucini to someone who hasn't figured out how to boil water yet.
     
  13. duny thread starter macrumors newbie

    duny

    Joined:
    Sep 21, 2008
    Location:
    H-Town

Share This Page