This article reprinted to http://blog.csdn.net/chen505358119/article/details/9278539Here I summarize the way the data is stored, one is to save in the sandbox to create the file, then write the data to the file, the second is saved to Plist, and the third is saved to the database.
1. Save to file requires Nskeyedarchiver and Nskeyedunarchiver
Save data: Nsarray*paths=nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmas K,yes);
NSString *path = [pathsobjectatindex:0];
Nsmutabledata *data =[[nsmutabledata alloc] init];
Nskeyedarchiver*archiver = [[Nskeyedarchiver alloc]
Initforwritingwithmutabledata:data];
[Archiver encodeobject:_filletext.text forkey:@ "Ty"];
[Archiver finishencoding];
[Data writetofile:[path stringbyappendingpathcomponent:@ "test"]
Atomically:yes];
[Archiver release];
[Data release];
Get Data:
Nsarray *paths =nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask,yes);
NSString *path = [pathsobjectatindex:0];
Nsmutabledata *data =[[nsmutabledata alloc] Initwithcontentsoffile:
[Path stringbyappendingpathcomponent:@ "test"];
Nskeyedunarchiver *unarchiver =[[nskeyedunarchiver Alloc]
Initforreadingwithdata:data];
Nsobject*ret = [[unarchiverdecodeobjectforkey:@ "Ty"] retain];
[Unarchiver finishdecoding];
[Unarchiver release];
[Data release];
It can also be written like this only the previous one was serialized:
Save:
Nsarray *paths =nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask,yes);
NSString *path = [pathsobjectatindex:0];
[Nskeyedarchiver archiverootobject:_filletext.text tofile:[pathstringbyappendingpathcomponent:@ "test"];
Get:
Nsarray *paths =nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask,yes);
NSString *path = [pathsobjectatindex:0];
nsstring* str=[nskeyedunarchiver unarchiveobjectwithfile:[pathstringbyappendingpathcomponent:@ "test"];
2. Save to the Plist file
There are two types that are stored in the system's own plist, and the other is saved in the plist file that you created.
Save to the system's own plist
Save:
[[Nsuserdefaults Standarduserdefaults] setobject:_filletext.textforkey:@ "Save"];
[[Nsuserdefaults standarduserdefaults]synchronize];
Get Data:
Nsstring*str=[[nsuserdefaults Standarduserdefaults] objectforkey:@ "Save"];
Save to a self-created plist
Save:
nsmutabledictionary* dict=[nsmutabledictionarydictionarywithobjectsandkeys:@ "Chen", @ "sex", @ "Liang", @ "name", Nil] ;
Nsarray *paths = Nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask, YES);
NSString *docpath = [pathsobjectatindex:0];
NSString *myfile = [DocPath stringbyappendingpathcomponent:@ "User.plist"];
[Dict writetofile:myfile Atomically:yes];
Get Data:
Nsarray *paths = Nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask, YES);
NSString *docpath = [Paths objectatindex:0];
NSString *myfile = [DocPath stringbyappendingpathcomponent:@ "User.plist"];
nsdictionary* dic = [Nsdictionary dictionarywithcontentsoffile:myfile];
But I write, I think the first and the second kind of similar, basically can be categorized as a save database to a file, the difference is saved to the plist can open to see the data inside, the first kind of invisible, so more secure.
3. Save the data to the database, generally divided into two kinds of iOS comes with the database CoreData, and the other is sqllite, I want to say is because sqllite a lot and C association, if c is not very familiar with, suggest to use fmdatabase this third party library, It is the package of sqllite, very useful, for the database this piece next time write
What's the problem, please give us more advice
iOS about data storage