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

newlearner

macrumors member
Original poster
Jul 30, 2009
37
0
india
hello all,

i want to retreive the files which have been written into the applications documents directory. I have used this code to write the files:

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

NSString *documentsDirectory = [paths objectAtIndex:0];
	
NSString *FilePath = [documentsDirectory stringByAppendingPathComponent: FileName];    
	
NSURL *url = [NSURL fileURLWithPath:FilePath];


Now i want to retreive all the written files from Documents and display them into a table. So, how to get back all the files in an array?

thanks
 
the first line in your code retrieves all the file names, including paths, in an array of strings. you will normally to iterate thru this array to display the file names, although in your case you may not have to. i would suggest following the recipes in "Table View programming guide for the iPhone OS" (part of the iphone os sdk) - they should be quite straightforward for what you need. you probably wont need to use anything else than the 1st line in your code.
 
Look at the NSFileManager docs for ways to manipulate the file system from Cocoa.

Code:
NSArray*	fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:inPath error:nil];
 
thank you all,

i am able to retrieve the file names using the following code:

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

NSString *documentsDirectory = [paths objectAtIndex:0];

NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
 
it's ok, but it's fragile. reason is, your documents directory may not always be at index 0 in your paths array. you should iterate over your paths array until you find your documents directory, then go after the files in it. this way, you're covered.
 
@drivefast, I don't think so. There has never been more than one Documents folder per app on the phone. What criteria do you use to tell if you've found your Documents folder?

I've never seen any other code suggested for this purpose.
 
@drivefast, I don't think so. There has never been more than one Documents folder per app on the phone.

Bad idea to hardcode that kind of assumption because if Apple changes that in the future, your app may break or have unexpected behavior. In about 20 years of Mac development, I've learned to try to never assume a particular implementation will always remain that way -- the hard way. :D
 
@electroshock and @drivefast, you didn't answer my question

What criteria do you use to tell if you've found your Documents folder?

Or to put it more simply, if that code is wrong what code is right?
 
i'm usually concatenating the response of NSHomeDirectory() with "/Documents", and in most cases with a filename, because i usually know what file i'm looking for. sort of like this:
Code:
NSString *myFileFQN = [NSString stringWithFormat:@"%@/Documents/%@", NSHomeDirectory(), myFileName];
i write the files building the fqn's as such, and i read them back the same, so this sounds like safe enough to me.

i didnt said the OP's method method was wrong, i just said it seems fragile, and this is a personal opinion. if the result of a function that i call is an array of paths, this is a strong suggestion to me that i should go and look for what i need into what's returned, rather than assuming that the array only contains one element, or what i need will always be in the first position.
 
Hardcoding the Documents folder name is fragile. On iPhone OS that folder isn't displayed to the user so the name could be anything that Apple wants. The OP's code will always work if they change the name. Not yours.

The reason that the code returns an array is because this function is shared with MacOS X and can be used to return a bunch of different system folders. On iPhone OS developers don't have access to most of those folders, and they may not exist.

Really, apple should provide an iPhone-only single API that returns the user's Documents folder.

You might want to look at this apple page where they say that doing it the way you suggest is not preferred but doing it the way the OP does is preferred, under User Directories: http://developer.apple.com/mac/libr...hs.html#//apple_ref/doc/uid/20000152-BBCBIGHH

See also: http://developer.apple.com/mac/libr...Directories.html#//apple_ref/doc/uid/20001279
 
myeah... i'll admit it since it's official recommendation, but i dont think i like their implementation :eek: i still dont feel like i'm in supposed to go back and modify all my code base to use the NSDocumentsDirectory method.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.