Here is one easy way to save data in your iPhone apps.
If you are using an array, say a list of strings, you can easily save that information to the documents directory of your app. In this example, we are going to create an array, fill it with values and then save the contents of the array to the documents directory.
Then, as we are going to stop the program and put in code that will retrieve the array from the filesystem. Finally, to prove that it all worked we will use an alert box to inform the user of what is in the array that we stored.
First, in the applicationDidFinishLaunching method put in this code:
//get the documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the
//documents directory:
NSString *fullFileName = [NSString stringWithFormat:@"%@/arraySaveFile", documentsDirectory];
//create an array and add values to it:
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:@"One"];
[array addObject:@"Two"];
[array addObject:@"Three"];
//this statement is what actually writes out the array
//to the file system:
[array writeToFile:fullFileName atomically:NO];
Now, your information has been saved to the iPhone’s file system in the documents directory of your app. Here is how you would retrieve the information that you saved:
//get the documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the
//documents directory:
NSString *fullFileName = [NSString stringWithFormat:@"%@/arraySaveFile", documentsDirectory];
//retrieve your array by using initWithContentsOfFile while passing
//the name of the file where you saved the array contents.
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:fullFileName];
//use an alert to display the first value in the array to prove
//that you were able to save and retrieve the information.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
message:[array objectAtIndex:0]
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
That is it – a quick and easy way to save information on the iPhone!
分享到:
相关推荐
You'll be given an overview of the available Yocto Project components, how to set up Yocto Project Eclipse IDE, and how to use tools such as Wic and Swabber that are still under development....
An introduction to the UNIX filesystem and its features One chapter on vim and one chapter on emacs A chapter on programming tools including sections on gcc, make, gdb, and CVS Detailed coverage of ...
The usage section outlines the operational aspects of the secure OTA update system, including how to initiate and manage updates. It includes: - **Initiating Updates**: How to trigger OTA updates ...
<END><br>55,Assist.zip A simple application with source code which shows how to save the contents of a rich text box without the help of common dialog box. <END><br>56,LocFiles.zip The attached...
filesystem, will add the source items to the existing filesystem. By default, the source items are added to the existing root directory. To make this clear... An existing filesystem "image" contains ...
The method used is a matter of preference, but *it will be very difficult to use this library without first understanding the JSON syntax for the API*, so it is recommended to look at the [APIs ...
the contents of the directory. For example: example 4: %mksquashfs /home/phillip/test output_fs -keep-as-directory This is the same as example 1, except for -keep-as-directory. This will generate a...
you can copy the entire contents of the disc to your hdd, we have thus far only tested this in windows explorer, were anxious to hear about how folks take things from here, better methods of ripping ...
- **Introduction to the Shell**: Explanation of the role of the shell in Unix-like systems and how it serves as the primary interface for interacting with the system. - **Common Shell Commands**: List...
Sections 2.3 and 2.4 describe the services provided by the 4.4BSD kernel, and give an overview of the latter's design. Later chapters describe the detailed design and implementation of these services ...
Learn to apply the nuances of the Go language, and get to know the open source community that surrounds it to implement a wide range of start-up quality projects Write interesting and clever but ...
There's a great deal of excitement surrounding the use of Linux in embedded systems -- for everything from cell phones to car ABS systems and water-filtration plants -- but not a lot of practical ...
Take advantage of the new filesystem library to work with files and directories Use the new utility additions to the standard library to solve common problems developers encounter including string_...
Docker provides the networking primitives that allow administrators to specify how different containers network with each application and connect each of its components, then distribute them across a...