Data Persistence, write simple objects locally, and write complex objects locally

Source: Internet
Author: User
Tags tmp folder
# Import "mainviewcontroller. H "# import" student. H "@ interface mainviewcontroller () @ end @ implementation mainviewcontroller-(ID) initwithnibname :( nsstring *) bundle :( nsbundle *) handle {self = [Super initwithnibname: nibnameornil Bundle: nibbundleornil]; If (Self) {// custom initialization} return self;}-(void) viewdidload {[Super viewdidload]; // do any additional setup after loadin G The View. // data persistence, local storage // 1. find the sandbox path of the current application using the code // function: parameter 1: Which folder in the sandbox is to be found // parameter 2: find a folder in the system (nsuserdomainmask = 1 can only be used in IOS) // parameter 3: whether it is an absolute path // nsarray * paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes); /// nslog (@ "Paths of documents: % @", paths); // the roles of the three folders // 1. documents folder: stores user settings and some data to be saved. // restrictions: if you are using the iCloud service, all the data in the documents file will be uploaded to the server. the space provided by the iCloud server is limited (5 GB ). Folder-based storage of audio/video/large price; // 2. library: -- caches; cache folder, storage system/users' cache files (for example, TXT/image/video/audio ..., clear cache is to delete this folder) // perferences Folder: it is a preference folder for developers to store some data required for application execution. // 3. TMP Folder: stores temporary files. All contents are deleted during version update. // 4. the executable file of the application (. APP), including all the code, resources, and Basic settings executed by this application, (which cannot be modified at all) (nsbundle) // obtain an image path in the app // parameter 1: file Name of the resource // parameter 2: file type of the resource // nsstring * ImagePath = [[nsbundle mainbundle] pathforresource: @ "1-121126113549" oftype: @ "jpg"]; // nslog (@" % @ ", ImagePath); // uiimageview * imageview = [[uiimageview alloc] initwithframe: cgrectmake (0, 0,320,480)]; // two Image Display Methods; // uiimage * image = [uiimage imagewithcontentsoffile: ImagePath]; // uiimage * image1 = [uiimage imagenamed: @ "1-121126113549.jpg"]; // imageview. image = [uiimage imagewithcontentsoffile: ImagePath]; // [self. view addsubview: imageview]; // imagenamed loads the image and caches it into the memory. // imagewithcon Tentsoffile only displays images, but does not add them to the memory. // Therefore, it is better to use imagewithcontentsoffile when loading a large number of images, and the memory will not increase. // -------------- Write a simple object locally -- _-_ ------------ // nsstring, nsarray, nsdictionary, and the object generated by the data volume class provided by the system itself, it is called a simple object // nsstring string to write a local nsstring * string = @ "it's okay not to ask for leave! "; // Method provided by the system to directly write data to the local device // concatenate the file path // obtain the libary folder path nsstring * libpath = [nssearchpathfordirectoriesindomains (nslibrarydirectory, nsuserdomainmask, yes) lastobject]; nsstring * strpath = [libpath stringbyappendingstring: @ "/string.txt"]; nslog (@ "Path of the string file % @", strpath); // parameter 1: file Path parameter 2: whether to protect the stored process parameter 3: utf8 parameter 4: error message () nserror * error = nil; [String writetofile: strpath atomically: Yes encoding: nsutf8stri Ngencoding error: & error]; If (error! = Nil) {nslog (@ "error message: % @", error);} // nsstring * readstr = [nsstring stringwithcontentsoffile: strpath encoding: nsutf8stringencoding error: nil]; nslog (@ "% @", readstr); // nsarray * array = [nsarray arraywithobjects: @ "AA", @ "BB ", @ "cc", @ "DD", @ "ee", @ "FF", nil]; // nsstring * arraypath = [libpath stringbyappendingpathcomponent: @ "array. plist "]; [array writetofile: arraypath atomically: Yes]; // read nsarray * readarray = [nsarray arraywithcontentsoffile: arraypath]; nslog (@" % @ ", readarray ); // use the nsuserdefaults class for local storage // This is a dictionary that can be saved in the local system // use a singleton method to create this object // singleton: Design Mode: in a program running, only one object nsuserdefaults * userdefaults = [nsuserdefaults standarduserdefaults] is generated; // use [userdefaults setobject: @ "alksdjflkasd" forkey: @ "key"]; // write it to the local machine immediately [userdefaults synchronize]; // display (determine the First Login) nslog (@ "% @", [userdefaults objectforkey: @ "key"]); // determine whether the user is using the application for the first time if ([[nsuserdefaults standarduserdefaults] objectforkey: @ "isfirstlogin"] = nil) {// if you use [nsuserdefaults standarduserdefaults] setobject for the first time: @ "yes" forkey: @ "isfirstlogin"]; [[nsuserdefaults standarduserdefaults] synchronize];} else {// not the first time} // 4. write complex objects to local student * Stu = [[STUDENT alloc] init]; Stu. name = @ "maidong"; Stu. sex = @ "NV"; Stu. address = @ "liaoningdalian"; // archive class, write a complex object locally (only available in the nscoding protocol) // parameter 1: the object you want to store (must implement the <nscoding> protocol) // parameter 2: storage path nsstring * stupath = [libpath stringbyappendingstring: @ "student. suiyi "]; [nskeyedarchiver archiverootobject: Stu tofile: stupath]; [STU release]; // read // unarchive class student * sturead = [comment unarchiveobjectwithfile: stupath]; nslog (@ "Name: % @, Gender: % @, address: % @", sturead. name, sturead. sex, sturead. address); // process for storing complex objects // 1. creates a class to implement the nscoding protocol and decodes all attributes. // 2. archive and write a Class Object locally using the archive class (nskeyedarchinver) // 3 use the archive class (nskeyedunarchiver) to archive an archive file in a path, generate a new object}-(void) didreceivememorywarning {[Super didreceivememorywarning]; // dispose of any resources that can be recreated .} /* # pragma mark-Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation-(void) prepareforsegue :( uistoryboardsegue *) segue sender :( ID) sender {// get the New View Controller Using [segue destinationviewcontroller]. // pass the selected object to the New View Controller .} * // @ end


This article from the "Liu _ blog" blog, please be sure to keep this source http://liuyafang.blog.51cto.com/8837978/1553016

Data Persistence, write simple objects locally, and write complex objects locally

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.