Data persistence (sandbox, data storage)

Source: Internet
Author: User

Steps for Data persistence
// 1.指定前往哪个文件夹// 2,用字符串接收路径// 3.拼接文件夹路径// 4.写入本地或者归档操作// 注 :如果是复杂对象归档 ,要签订NSCoding方法 .并且实现两个协议方法,放在数组里的复杂对象归档也要签协议
Apple mobile phone in order to ensure the absolute security of their data on the design of the sandbox files, each application is equipped with its own sandbox files, each run, the name of the folder will become a string without any regularity the first parameter is currently going to which folder, go to documents with NSDocumentDirectory, 64 line that, you can also go to the caches folder, corresponding to 68 rows of the second parameter to access the folder type, specify the user folder when access the third parameter absolute path (YES), relative Path (NO) (1) absolute path is used for the system, the system can To find the folder according to the current path, we use the absolute path when manipulating the file (2) The relative path will only show the folder to go to, the other part is ~, tell the programmer to which folder
NSArrayYES);
There's a total of three files in the sandbox.
1.是Documents文件 : 主要是用来储存用户的想要储存的一些信息 ,比如收藏的信息或者自己设置的一些内容 ,所以我们做收藏功能就是前往这个文件夹里写文件2.Library文件夹是方便程序员开发使用的.主要操作它里面的两个文件夹 ,caches和 preferences(1).caches :用来保存缓存文件 ,SDWebImage会把图片加到缓存文件中 ,所以清除缓存功能就是把这个文件夹删除(2).preferences 一般来保存程序员设置的信息,比如NSUserFaults就会把数据保存在这个文件夹里3.tmp文件:一般存放临时内容之前在沙盒里还有一个.app文件 ,在新的版本里已经被移走了
Write a simple object to the local, simple object refers to NSString, Nsarray, etc. 1. Get the sandbox path by array first
NSArrayYES);
2. Get the sandbox path from the array
NSString *sandBoxPath =sandBox[0];
3. To stitch a path to the written file, there are two ways to stitch
NSString *documentPath =[sandBoxPath stringByAppendingPathComponent:@"顾宇.xml"];
Writes a string to a local

NSString *str= @ "next door Lao gu";

First parameter: The path to save the file
Second parameter: Protect the file, yes
Third parameter: encoding
Four, error message

error:nil];
If the path has a corresponding file, the original file will be overwritten, if not, create a new file to read the sandbox file again
NSString *tempStr =[NSString stringWithContentsOfFile:documentPath encoding:NSUTF8StringEncoding error:nil];
Writes an array to the local

Nsarray *arr [Email protected][@ "1", @ "2", @ "3", @ "4", @ "5", @ "6"];

1. Get the sandbox address through an array

NSArrayYES);

2. Save the sandbox path with a string

NSString *sandBoxPath =sandBox[0];

3. To the file to be written stitching path

NSString *documentPath =[sandBoxPath stringByAppendingPathComponent:@"gu.plist"];

4. Write the array to the local

[arr writeToFile:documentPath atomically:YES];

5. Read the array again

NSArray *temp =[NSArray arrayWithContentsOfFile:documentPath];
Write a simple dictionary to the local
 nsdictionary*dic =[nsdictionarydictionarywithobjectsandkeys:@"1",@"2",@"3",@"4",@"5",@"6",Nil];Nsarray*sandbox =nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask,YES);NSString*sangboxpath =sandbox[0];NSString*documentpath =[sangboxpath stringbyappendingpathcomponent:@"111.plist"];NSLog(@"%@", documentpath); [DiC Writetofile:documentpath atomically:YES];nsmutabledictionary*temp =[nsmutabledictionaryDictionarywithcontentsoffile:documentpath];NSLog(@"%@", temp);

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Data persistence (sandbox, data storage)

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.