Objective-c Simple encapsulation String storage operation, eliminates the middle sandbox processing way
Copy Code code as follows:
/Storage PublicKey and SessionID-writecontent:nil-only fetch data, others-modify the original content and extract
+ (NSString *) StoreFile: (NSString *) fileName content: (NSString *) writecontent
{
NSString *pathdocuments=[nssearchpathfordirectoriesindomains (NSDocumentDirectory, NSUserDomainMask, YES) OBJECTATINDEX:0];
NSString *createpath=[nsstring stringwithformat:@ "%@/%@", pathdocuments,filename];//fill full path with filename
Nserror *readfileerror;
NSString *readcontent;
NSData *data;
if ([[[[Nsfilemanager Defaultmanager] fileexistsatpath:createpath])//Determine if the file already exists
{
if (nil = = writecontent) {
ReadContent = [NSString stringwithcontentsoffile:createpath encoding:nsutf8stringencoding error:&ReadFileError;];
}else{
data = [Writecontent datausingencoding:nsutf8stringencoding];//of new files
[[Nsfilemanager Defaultmanager] Createfileatpath:createpath contents:data attributes:nil];//Create file
ReadContent = [NSString stringwithcontentsoffile:createpath encoding:nsutf8stringencoding error:&ReadFileError;];
}
}
Else
{
if (nil = = writecontent) {
return nil;
}else{
data = [Writecontent datausingencoding:nsutf8stringencoding];//of new files
[[Nsfilemanager Defaultmanager] Createfileatpath:createpath contents:data attributes:nil];//Create file
ReadContent = [NSString stringwithcontentsoffile:createpath encoding:nsutf8stringencoding error:&ReadFileError;];
}
}
return readcontent;
}
+ (Nsarray *) Storearryfile: (NSString *) fileName content: ( Nsarray *) Writearry
{
Steps
Saving: Putting an array into a dictionary
Fetching: Fetching an array from a dictionary
if (Writearry = = nil)//Read file
{
NSString *storestr = [fnprorequest storefile:filename content:nil];
Nsdictionary *dic = (nsdictionary *) [Storestr Jsonvalue];
NSLog (@ "%@", DIC);
Return (Nsarray *) [dic objectforkey:filename];
}
Else
{
Nsarray *objectsarry = [[Nsarray alloc]initwithobjects:writearry,nil];
Nsarray *keysarry = [[Nsarray alloc]initwithobjects:filename,nil];
Nsdictionary *dic = [[Nsdictionary alloc]initwithobjects:objectsarry Forkeys:keysarry];
NSString *storestr = [dic jsonrepresentation];
[Self storefile:filename content:storestr];
[Objectsarry release];
[Keysarry release];
[DIC release];
}
return nil;
}