Objective C encapsulates an example of a string storage operation

  • 2020-05-06 11:42:41
  • OfStack

Es2en-C simply encapsulates string storage operations, eliminating the middle sandbox handling


/ storage publickey and sessionID -- writeContent :  nil  -   Just take out the data,   other   -   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];// Complete the path with the filename 
    NSError *ReadFileError;
    NSString *readContent ;
    NSData *data ;
    if ([[NSFileManager defaultManager] fileExistsAtPath:createPath])// Determines if the file already exists 
    {
        if (nil == writeContent) {
            readContent = [NSString stringWithContentsOfFile:createPath encoding:NSUTF8StringEncoding error:&ReadFileError;];
        }else{
            data = [writeContent dataUsingEncoding:NSUTF8StringEncoding];// The initial data for the new file 
            [[NSFileManager defaultManager] createFileAtPath:createPath contents:data attributes:nil];// Create a file  
            readContent = [NSString stringWithContentsOfFile:createPath encoding:NSUTF8StringEncoding error:&ReadFileError;];            
        }
    }
    else
    {
        if (nil == writeContent) {
            return nil;
        }else{
            data = [writeContent dataUsingEncoding:NSUTF8StringEncoding];// The initial data for the new file 
            [[NSFileManager defaultManager] createFileAtPath:createPath contents:data attributes:nil];// Create a file 
            readContent = [NSString stringWithContentsOfFile:createPath encoding:NSUTF8StringEncoding error:&ReadFileError;]; 
        }
    } 
    return readContent;
}
+ ( NSArray * )storeArryFile:(NSString *)fileName content:( NSArray *)writeArry
{
    // steps 
    // save   Will:   The array into the   The dictionary 
    // Pick up:   Take the array from the dictionary 
    if ( writeArry == nil)// Read the 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 articles: