-
Writing data to a plist file
-
//Get Path Object NSArray *pathArray = Nssearchpathfordirectoriesindomains (Nsdocumentdirectory, nsuserdomainmask, yes); nsstring *path = [patharray objectatindex:0]; //get the full path to the file nsstring *filepatch = [path stringbyappendingpathcomponent:@ " Xiaoxi.plist "]; //above 3 sentences can be written this sentence// nsstring *filepatch = [[nssearchpathfordirectoriesindomains (Nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0]stringbyappendingpathcomponent:@ "Xiaoxi.plist"]; // nslog (@ "------filepath---%@", filepatch); /* * below is my plist path, click in the blank of the desktop, go to-hold the option-resource library-developer-coresimulator-devices ...... Just follow the path below to find where plist is located * /users/baiteng01/library/developer/coresimulator/devices/92444384-5241-4934- B078-1a7241f1b687/data/containers/data/application/73005382-d1fb-4bc2-bb4e-1fbc64284141/documents/xiaoxi.plist * */ //writing data to plist file nsmutabledictionary *dic1 = [nsmutabledictionary dictionarywithobjectsandkeys:@ "Little Tiger", @ "name", @ "5", @ "age", @ "boy" @ "Sex",nil]; NSMutableDictionary *dic2 = [NSMutableDictionary dictionarywithobjectsandkeys:@ "Little" @ "name" @ "6" @ "Age" @ "girl" @ "Sex",nil]; //Save the above 2 dictionary into the large dictionary nsmutabledictionary *datadic = [ nsmutabledictionary dictionary]; [datadic setobject:dic1 forkey:@ "First grade"]; [datadic setobject:dic2 forkey:@ "Second Grade"]; //write plist inside [datadic writetofile:filepatch atomically:yes]; //read the contents of the Plist file nsmutabledictionary *datadictionary = [[nsmutabledictionary alloc] Initwithcontentsoffile:filepatch]; nslog (@ "---plist at the beginning of the preservation of content---%@", datadictionary);
-
plist file Contents/delete/modify/Add/write operations
-
Modify the contents of the dictionary, first according to the structure you want to modify the content of the dictionary nsmutabledictionary *dd = [datadictionary objectforkey:@ "first grade"]; [DD setobject:@ "I changed my name, Oh," forkey:@ "name"]; [DD setobject:@ "New content I added" forkey:@ "content"]; [DD removeobjectforkey:@ "age"]; After the modification succeeds, add this dictionary back to the large dictionary [datadictionary setobject:dd forkey:@ "first grade"]; [Datadictionary Writetofile:filepatch Atomically:yes]; NSLog (@ "---plist after the operation of the dictionary content---%@", datadictionary);
-
Delete plist file
-
//purge plist files, I can go in the way I told you. See if the plist file is cleared locally nsfilemanager *filemger = [nsfilemanager defaultmanager]; nsstring *xiaoxipath = [[ Nssearchpathfordirectoriesindomains (Nsdocumentdirectory, nsuserdomainmask, yes) objectAtIndex:0] stringbyappendingpathcomponent:@ "Xiaoxi.plist"]; //if the file path exists BOOL bRet = [fileMger fileExistsAtPath:xiaoXiPath]; if (BRet) { NSError *err; [filemger removeitematpath:xiaoxipath error: &err]; }
Write/read/delete of IOS plist file operations