What about the plist file?

Source: Internet
Author: User

Today, new students ask a question, that is, you define a plist file, and then you can write things in it, but write once again to run the data is always the last time to write data. Later, I studied the plist file specially.

We all know that when you create a project, there will be a info.plist file in supporting files, and the information stored in this file is as follows:

It is a configuration file for the entire app, including information such as version information and app desktop display name. Maybe I don't know yet, but after my test I found I couldn't manipulate the Info.plist file with code.

I think it's necessary to put the plist file aside and get a look at the file directory first. See directly:

I have already marked the use of the folder and the role of the file, I believe you should have a complete directory structure to understand. Now, let's talk about the plist file.

Example: I want to make an address book to use plist file to store everyone's basic information, including name and phone number. The final effect you want to achieve is:

Then I can do this:

- (void) viewdidload {[Super viewdidload]; NSString*rootpath =nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES). Firstobject; NSString*path = [RootPath stringbyappendingpathcomponent:@"user.plist"]; Nsmutablearray*totalarr =[[Nsmutablearray alloc] initwithcontentsoffile:path]; if(!Totalarr) {Totalarr= [NsmutablearrayNew]; } nsmutabledictionary*dic = [NsmutabledictionaryNew]; [DiC setobject:@]183434534535"forkey:@"Telephone"];[DiC setobject:@ "Harry"forkey:@ "Name"];    [Totalarr Addobject:dic];   [Totalarr Writetofile:path Atomically:yes]; } 

First step: I will first get the directory that contains the files that we created.

NSString *rootpath = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES). FirstObject;

The second step: the equivalent of string concatenation, get the absolute path of the user.plist.

NSString *path = [RootPath stringbyappendingpathcomponent:@ "user.plist"];

Step three: I create a new nsmutablearr with the data from the file pointed to by path, the plist file. In this case, the data in the file can be written to Totalarr.

Nsmutablearray *totalarr = [[Nsmutablearray alloc] initwithcontentsoffile:path];     if (! Totalarr)    {        new];    }

Note that there may be user.plist file does not exist, so if this file does not exist, then Totalarr is not created successfully, so add a judgment, if Totalarr is not created successfully (because there is no file under path), create a new one.

Fourth step: Create a new dictionary and put the dictionary in the Totalarr.

Nsmutabledictionary  new];    [DiC setobject:@] 183434534535 " forkey:@ "telephone"];     [dic setobject:@ "Harry "   forkey:@ "name"];    [Totalarr Addobject:dic];     // Here I add the newly created DIC to the original array so that the original data is also written to the plist file.
View Code

Fifth step: Write the Totalarr to the plist file.

[Totalarr Writetofile:path Atomically:yes];   

Note: If the file does not exist in path, the WriteToFile method will automatically create a user.plist file. The data in the Totalarr is then written in.

So it's done.

Note: The Plist file is written (that is, [Totalarr Writetofile:path Atomically:yes];), the entire plist file is emptied first, and then the Totalarr is written to it from the new, so in order to save the data originally written, It is necessary to add a new dic ([Totalarr Addobject:dic]) on the basis of the original array, so that the original data can be written in a single piece.

Maybe you would like to create a new plist file directly in supporting files? The answer is no. The specific reasons may be as follows:

As you can see, my new plist file is not in the sandbox at all under the project folder. And although I can read the file (with nsbundle operation), it is not clear that the test found that it cannot be manipulated.

But I use

NSString *testpath = [[NSBundle mainbundle] Pathforresource:@ "test" ofType:@ " plist "];

To get Testpath, find its path as follows:

/users/admin/library/developer/coresimulator/devices/f73d2947-21cc-4e8e-ae3e-d80e6652d840/data/containers/ Bundle/application/c77e606f-ab5b-425a-bd2f-bc7b6b078187/test.app/test.plist

Careful observation, originally this file is Test.app in the file, this is also reasonable, because this test.app is the application. I used the above writing method to write data, I found that I opened Test.app test.plist. There is data in it, which means I wrote the data, but when I look at the test.plist in the supporting files folder, there is no data. And at the time of running the program, the Test.app will regenerate. The data in it has disappeared.

What about the plist file?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.