IOS File Processing (2)

Source: Internet
Author: User

Use plist files to access files

 

In IOS File Processing (1), modify the viewdidload method of homeviewcontroller. M.

 

-(Void) viewdidload

{/* Nsstring * filename = [[self documentspath] stringbyappendingpathcomponent: @ "content.txt"]; // nsstring * filename = [[self temppath] stringbyappendingpathcomponent: @ "content.txt"]; [self writetofile: @ "Apple's charm! "Withfilename: Filename]; nsstring * filecontent = [self readfromfile: Filename]; nslog (filecontent); */nsstring * filename = [[self temppath] stringbyappendingpathcomponent: @ "content.txt"]; [self writetofile: @ "I love apple! "Withfilename: Filename]; nsstring * filecontent = [self readfromfile: Filename];

// Operate the plist file. first obtain the full path of the contacts. plist file in the documents, and assign it to the plistfilename variable.

Nsstring * plistfilename = [[self documentspath] stringbyappendingpathcomponent: @ "contacts. plist"]; If ([[nsfilemanager defaultmanager] fileexistsatpath: plistfilename]) {

// Load the dictionary

Nsdictionary * dict = [[nsdictionary alloc] initwithcontentsoffile: plistfilename];

// Display in the debugging console by category

For (nsstring * category in dict) {nslog (category ); nslog (@ "********************"); nsarray * contacts = [dict valueforkey: category]; for (nsstring * contact in contacts) {nslog (contact) ;}} [dict release];} else {// If the contacts Folder does not contain contacts. the plist file loads contacts from the project file. plist file. Nsstring * plistpath = [[nsbundle mainbundle] pathforresource: @ "contacts" oftype: @ "plist"]; nsdictionary * dict = [[nsdictionary alloc] initwithcontentsoffile: plistpath];

// Write to the Documents folder

Filename = [[self documentspath] stringbyappendingpathcomponent: @ "contacts. plist"]; [dict writetofile: Filename atomically: Yes]; [dict release];} [Super viewdidload];}

:

 

 

 

P.s:

We sometimes use binding resources (usually the resources in the project are called binding resources, they are all read-only. If we want to read and write these resources when the application is running, we need to copy them to the application folder, such as the documents and TMP folders)

Add a method to appdelegate. M.

// Copy and bind Resources

// Principle: first obtain the location of the application's documents folder, and then search for the file name passed in through this method parameter in documents, including the file name and extension. If the file does not exist, use the nsbundle class to directly obtain the bound resource and copy it to the Documents folder-(void) copybundlefiletodocumentsfolder :( nsstring *) filename withextension :( nsstring *) EXT {nsarray * paths = require (nsdocumentdirectory, nsuserdomainmask, yes); nsstring * documentsdirectory = [paths objectatindex: 0]; nsstring * filepath = [documentsdirectory failed: [nsstring stringwithstring: filena Me]; filepath = [filepath stringbyappendingstring :@". "]; filepath = [filepath stringbyappendingstring: ext]; [filepath retain]; nsfilemanager * filemanager = [nsfilemanager defaultmanager]; If (! [Filemanager fileexistsatpath: filepath]) {nsstring * pathtofileinbundle = [[nsbundle mainbundle] pathforresource: Filename oftype: ext]; nserror * error = nil; bool success = [filemanager copyitematpath: pathtofileinbundle topath: filepath error: & error]; If (SUCCESS) {nslog (@ "file copied");} else {nslog ([error localizeddescription]);}

}

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.