OBJECTIVE-C Encapsulation String Storage Operation example _ios

Source: Internet
Author: User

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;
}

Related Article

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.