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

duny

macrumors newbie
Original poster
Sep 21, 2008
26
0
H-Town
Can someone show me the code for the
.h and .m ViewControl to obtain the files in a directory.
 
Look at directoryContentsAtPath, which is part of NSFileManager. This will return an array.
 
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.
 
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];
		}
	}
 
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.
 
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'
 
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.
 
Cant you just help me out. The iphone dev book doesn't come out till January.
 
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
 
As an Amazon Associate, MacRumors earns a commission from qualifying purchases made through links in this post.
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.