Which file format should I use to save an array to a file? - iphone

I am using the solution found here: Save NSArray to File and I am just wondering what type of file format I should save my array as. Can I just use .txt?

Any format. iOS writeToPath: does not read the extension. You can even make up weird file names.
Take the codes from your reference question as example,
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:FILE_NAME];
[myArray writeToFile:filePath atomically:YES];
the FILE_NAME could be in any ASCII values.

The writeToFile of an array object creates a property list file. So, while you can use whatever extension you want, it's generally good style to use the plist extension.

Related

How do I clear the data in a plist created in Xcode?

I have been using a plist to store data in my app. I have been able to write and read from the plist with no problem. I created this plist in Xcode, adding the rows of numbers, dictionaries, and arrays myself. However, I would like to be able to reset the plist to the original state, and there must be an easier way to do this than writing a 0 or nil value to every entry in the plist. So what is the easiest way to reset the plist to its initial default state?
The simplest thing would be to delete the file using NSFileManager, like this:
[[NSFileManager defaultManager] removeItemAtPath:plistPath error:NULL];
Or if you don't want to do that, assuming the plist is a dictionary, just load the one from your application bundle and then overwrite the one in your documents, like this:
NSDictionary *originalPlist = [NSDictionary dictionaryWithContentsOfFile:bundleFile];
[originalPlist writeToFile:documentsFile atomically:YES];
Which will overwrite the saved file with the original file.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:#"mobile-watchlist.plist"];
[fileManager removeItemAtPath: fullPath error:NULL];
You could also try to just rename your Plist. Thats the least work i think.

How to get the directory tree of iPhone App Documents Directory

In my App, I created subfolders under Documents, and subfolders under subfolders. So there is a Document tree. I want to get the tree in a way that I am able to list them in a tableview. They will look like the following, linear listing with different indent to show their level.
I am not sure whether there is some convenient way to get this done or not, when I am trying to do it using while loop and for loop.
Appreciate your suggestions.
Thanks.
Apple provides a number of classes for file system access, and even some for recursion that you're trying to do.
I suggest that you take a look at the Apple File System Programming Guide and the NSFileManager class documentation. Additionally, the NSDirectoryEnumerator class may help as well.
I do not know how to get entire hierarchy at a time. But following method can be helpful.
1 Use this to get documents derectory.
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [paths objectAtIndex:0];
2 use - (NSArray *)contentsOfDirectoryAtPath:(NSString )path error:(NSError *)error for getting list of directories in document directory.
Read this Class refrence for more details.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html
3 Using following method you can make path of internal directory.
NSString *directoryPath = [documentPath stringByAppendingPathComponent:[MAIN_DIRECTORY stringByAppendingPathComponent:filePath]];
Using above three can together you can brows content of document directory.
Hope this is helpful
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
and
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *fileList = [manager directoryContentsAtPath:documentsDirectory];
for (NSString *s in fileList){
NSLog(s);
}

When saving something to disk with NSKeyedArchiver, where is that stored on the iPhone?

Example: If I used this, where does the iPhone store the file?
if (![NSKeyedArchiver archiveRootObject:your_object toFile:#"filename.plist"])
// save failed.
On my mac the filename.plist goes to Macintosh HD directly. No folder. Also, the NSKeyedArchiver doesn't seem to ask for a path, just for a file name. Strange or not?
And how's that file backed up with iTunes?
Well that actually depends on where you ask it to save the file, in your case, you're telling
it to store filename.plist, which will be stored in your root / .
if you want to store it in a specific location , just give it the full path, something like what Diederik Hoogenboom told you (this will store it in the Documents dir)
OR just give it the absolute path you want to save the file to.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent: #"myFile.plist"];
[NSKeyedArchiver archiveRootObject:your_object toFile:filePath];
This should probably have cleared it up a little for you.
archiveRootObject:toFile: (has been deprecated).
So you should pass it a path instead of just a filename. Otherwise NSKeyedArchiver will assume you want to store it in the root of the devices hard disk.
Update:
With the data result generated with NSKeyedArchiver, use the Data.write(to:options:) instance method which writes the data object's bytes to the location specified by a given URL.
The proper location to store the files is the documents folder:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

How to grab property list file created in Xcode?

I am having a hard time figuring out how to get the property list file that was created using Xcode. I created a property list file using array with NSString members. I want to grab that file and get all the NSString members and save it to a UITextField. But my problem is that I can't see that file. I don't know if I'm looking in the wrong path, or I don't know where the property file is saved.
Most likely, if the file was added via Xcode, the file is in your bundle. To open it use:
NSDictionary *plist = [NSDictionary arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"NameOfFile" ofType:#"plist"]];
Run this in the simulator and then look in the Finder, in Library/Application Support/iPhone Simulator/Applications/... to see if you can find the file, open it in and double check it to make sure Xcode added the file.
To make a copy in your Documents folder:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"My.plist"];
if (![plist writeToFile:path atomically:YES])
NSLog(#"not successful in writing the high scores");

NSDictionary WriteToFile fails

I can't work out why it keeps failing. It returns NO. I have searched this all afternoon, but nothing explains why or how to fix. Calling [[NSFileManager defaultManager] isWritableFileAtPath:] returns NO, which leads me to believe that the existing plist file can not be overwritten, but I can't work out how to change this.
You can write ONLY files in Documents directory. But your plist file is not there
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];