Summary of several ways to save the "learning summary" IOS data

Source: Internet
Author: User
<span id="Label3"></p>In the process of iOS development, no matter what the application, you will encounter the problem of data Preservation. Saving the data locally allows the program to run more smoothly without the appearance of an obnoxious chrysanthemum shape, making the user experience even Better. Here's How data is saved: <ol class="dp-cpp" start="1"> <ol class="dp-cpp" start="1"> <li>Nskeyedarchiver: in the form of an archive, the data object needs to adhere to the nscoding protocol, and the class corresponding to the object must provide Encodewithcoder: and Initwithcoder: methods. The previous method tells the system how to encode the object, and the latter method tells the system how to decode the Object.<br><br></li> <li class="alt">Nsuserdefaults: used to save application settings and properties, and user-saved Data. The data still exists after the user opens the program again or after powering it On. The types of data that Nsuserdefaults can store include: nsdata, nsstring, nsnumber, nsdate, nsarray, nsdictionary. If you want to store other types, you need to convert to the previous type to use Nsuserdefaults Storage.<br><br></li> <li class="alt">Write write method: permanently saved on Disk.<br><br></li> <li class="alt">Sqlite<br><br><br><br></li> </ol> </ol><p><p></p></p><p><p>4 ways to achieve</p></p><pre>@interface possession:nsobject<nscoding>{<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">compliance with nscoding protocol</span></span>NSString *name;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Pending Archive Type</span></span><span style="color: #000000;"><span style="color: #000000;">} @implementation Possession</span></span>-(<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) Encodewithcoder: (nscoder *<span style="color: #000000;"><span style="color: #000000;">) Acoder{[acoder encodeobject:name forkey:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">name</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; } </span></span>-(id) initwithcoder: (nscoder *<span style="color: #000000;"><span style="color: #000000;">) Adecoder{name</span></span>=[[adecoder decodeobjectforkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">name</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>] retain];<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;">self <span style="color: #000000;">; archive Operation: If you save the possession object Allpossession archive, you only need to nscoder the method Archiverootobject:tofile of the subclass Nskeyedarchiver: Yes. NSString</span></span>*path =<span style="color: #000000;"><span style="color: #000000;">[self possessionarchivepath]; [nskeyedarchiver archiverootobject:allpossessions tofile:path] Decompression operation: Also call the method of Nscoder subclass Nskeyedarchiver unarchiverootobject:tofile: You can allpossessions</span></span>=<span style="color: #000000;"><span style="color: #000000;">[[nskeyedunarchiver unarchiveobjectwithfile:path] retain]; Cons: Archive the form to save data, only one-time archive save and one-time Decompression. So only a small amount of data, and the data operation is clumsy, that is, if you want to change a small part of the data, still need to extract the entire data or archive the entire Data. </span></span></pre><p><p></p></p><p><p>Nsuserdefaults:</p></p><pre>Nsuserdefaults *defaults =<span style="color: #000000;"><span style="color: #000000;">[nsuserdefaults standarduserdefaults]; NSString</span></span>*name [email protected] "<span style="color: #0000ff;"><span style="color: #0000ff;">default</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">string</span></span><span style="color: #000000;"><span style="color: #000000;">"; [defaults setobject:firstname forkey:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">name</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Get UIImage instances</span></span><span style="color: #000000;"><span style="color: #000000;">UIImage</span></span>*image=[[uiimage alloc]initwithcontentsoffile:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">photo.jpg</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; NSData</span></span>*imagedata = UIImageJPEGRepresentation (image,<span style="color: #800080;"><span style="color: #800080;"></span> -</span>);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">convert UIImage objects to NSData</span></span><span style="color: #000000;"><span style="color: #000000;">[defaults synchronize];</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">persisting data to the Standarduserdefaults database using the Synchronize method</span></span><span style="color: #000000;"><span style="color: #000000;">Read Data: nsuserdefaults</span></span>*defaults =<span style="color: #000000;"><span style="color: #000000;">[nsuserdefaults standarduserdefaults]; NSString</span></span>*name = [defaults objectforkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">name</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>];<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">remove name based on key value</span></span>NSData *imagedata = [defaults dataforkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Image</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; UIImage</span></span>*image = [UIImage imagewithdata:imagedata];<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">NSData conversion to UIImage</span></span></pre><p><p></p></p><p><p></p></p><p><p>Write Writing Method:</p></p><pre><span style="color: #000000;"><span style="color: #000000;">Step one: Get the path to save the file: Nsarray</span></span>*documentpaths =<span style="color: #000000;"><span style="color: #000000;">nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask,yes); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Use the C function nssearchpathfordirectoriesindomains to get the full path of the directory in the Sandbox. The function has three parameters, a directory type, a he domain mask, and a Boolean Value. Where the Boolean value indicates whether the path needs to be extended through the ~. And the first parameter is constant, which is nssearchpathdirectory. In ios, the latter two parameters are the same: nsuserdomainmask and YES. </span></span>NSString *ourdocumentpath =[documentpaths objectatindex:<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">]; Another way is to use the Nshomedirectory function to get the sandbox path. The specific usage is: nsstring</span></span>*sandboxpath =<span style="color: #000000;"><span style="color: #000000;">nshomedirectory (); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Once You has the full sandbox path, you can create a path from it, but cannot write files on the sandbox file layer and cannot create directories, but should be based on creating a new writable directory, example such as Documents,library or Temp. </span></span>NSString *documentpath =<span style="color: #000000;"><span style="color: #000000;">[sandboxpath stringbyappendingpathcomponent:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Documents</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>];<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">add documents to the sandbox path for specific reasons before you analyze them! </span></span><span style="color: #000000;">the <span style="color: #000000;">difference between the two is that using Nssearchpathfordirectoriesindomains is more secure than adding a document behind Nshomedirectory. Because the file directory may change on future systems that are Sent. Step two: generate files under this path: nsstring</span></span>*filename=[documentdirectory stringbyappendingpathcomponent:filename];<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">filename <span style="color: #008000;">is the file name of the saved files</span></span><span style="color: #000000;"><span style="color: #000000;">Step three: Write data to the file: [data writetofile:filename atomically:yes];</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">writes the NSData type Object data to a file named filename</span></span><span style="color: #000000;"><span style="color: #000000;">last: read data from a file: nsdata</span></span>=[nsdata Datawithcontentsoffile:filename options:<span style="color: #800080;"><span style="color: #800080;">0</span></span>error:null];<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">read out data from filename</span></span></pre><p><p></p></p><p><p>Sqlite:</p></p><pre>First step: you need to add SQLite related libraries and header files: under build phases of the project file, locate the link Binary Library (ies) and add libsqlite3.<span style="color: #800080;"><span style="color: #800080;">0</span></span>. dylib (the difference between Libsqlite3.dylib and the former is not known, the two should be similar); add a header file to the header file or source file in the project file #import<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">/usr/include/sqlite3.h</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">Step two: Get started with Sqlite:nsarray</span></span>*documentspaths=<span style="color: #000000;"><span style="color: #000000;">nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, YES); NSString</span></span>*databasefilepath=[[documentspaths objectatindex:<span style="color: #800080;"><span style="color: #800080;">0</span></span>] Stringbyappendingpathcomponent:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">MyDB</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">the above two sentences are already more familiar with it! </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Open Database</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(sqlite3_open ([databasefilepath utf8string], &database) = =<span style="color: #000000;"><span style="color: #000000;">Sqlite_ok) {NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">SQLite dadabase is Opened.</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span>{<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>;}<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">turn on</span> unsuccessful to return</span><span style="color: #000000;">When the <span style="color: #000000;">database is open, if the database does not have a table, then you start to build the table oh! </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Char</span></span>*<span style="color: #000000;"><span style="color: #000000;">error; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Const</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Char</span></span>*createsql=<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">CREATE TABLE (id integer primary key autoincrement, name Text)</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(sqlite3_exec (database, createsql, null, null, &error) = =<span style="color: #000000;"><span style="color: #000000;">Sqlite_ok) {NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">CREATE TABLE is Ok.</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">error:%s</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, error); Sqlite3_free (error);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">empty the error string each time it is used, provided to the next use</span></span><span style="color: #000000;"><span style="color: #000000;">After the build table is complete, insert the record:</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Const</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Char</span></span>*insertsql=<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">INSERT into a person (name) values (' GG ')</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(sqlite3_exec (database, insertsql, null, null, &error) = =<span style="color: #000000;"><span style="color: #000000;">Sqlite_ok) {NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">insert operation is Ok.</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">error:%s</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, error); Sqlite3_free (error);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">empty the error string each time it is used, provided to the next use</span></span><span style="color: #000000;"><span style="color: #000000;">next, Query the Records:</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Const</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Char</span></span>*selectsql=<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Select Id,name from a</span> person</span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">; Sqlite3_stmt</span></span>*<span style="color: #000000;"><span style="color: #000000;">statement; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(SQLITE3_PREPARE_V2 (database,selectsql,-<span style="color: #800080;"><span style="color: #800080;">1</span></span>, &statement, Nil) = =<span style="color: #000000;"><span style="color: #000000;">Sqlite_ok) {NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Select operation is Ok.</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">error:%s</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, error); Sqlite3_free (error); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> while</span>(sqlite3_step (statement) = =<span style="color: #000000;"><span style="color: #000000;">Sqlite_row) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>_id=sqlite3_column_int (statement,<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">); NSString</span></span>*name= (<span style="color: #0000ff;"><span style="color: #0000ff;">Char</span></span>*) Sqlite3_column_text (statement,<span style="color: #800080;"><span style="color: #800080;">1</span></span><span style="color: #000000;"><span style="color: #000000;">); NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">row>>id%i, name%s</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, _id,name); } sqlite3_finalize (statement); finally, Close the database: sqlite3_close (db);<br><br><br></span></span></pre><p class="alt"><p class="alt">Note: to write to a database, the string can be char, and the char <span class="datatypes">type is removed from the database and <span class="datatypes">garbled when the <span class="datatypes">char type has a representation of the Chinese Characters. This is because the database uses ASCII encoding by Default.  therefore, in order to correctly remove the Chinese from the database, we need to use NSString to receive the string extracted from the Database. </span></span></span></p></p><pre><pre><span style="color: #000000;"></span></pre></pre><p><p></p></p><p><p>Summary of several ways to save the "learning summary" IOS data</p></p></span>

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.