Objective-C封裝字串儲存操作示例

NO IMAGE
1 Star2 Stars3 Stars4 Stars5 Stars 給文章打分!
Loading...

Objective-C簡單封裝 字串的儲存操作,省去中間沙盒處理方式

複製程式碼 程式碼如下:
/儲存publickey和sessionID — writeContent: nil - 僅取出資料, 其他 - 修改原內容並提取
(NSString *)storeFile:(NSString *)fileName content:(NSString *)writeContent
{
    NSString *pathDocuments=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSString *createPath=[NSString stringWithFormat:@”%@/%@”,pathDocuments,fileName];//用檔名補全路徑
    NSError *ReadFileError;
    NSString *readContent ;
    NSData *data ;
    if ([[NSFileManager defaultManager] fileExistsAtPath:createPath])//判斷檔案是否已存在
    {
        if (nil == writeContent) {
            readContent = [NSString stringWithContentsOfFile:createPath encoding:NSUTF8StringEncoding error:&ReadFileError;];
        }else{
            data = [writeContent dataUsingEncoding:NSUTF8StringEncoding];//新檔案的初始資料
            [[NSFileManager defaultManager] createFileAtPath:createPath contents:data attributes:nil];//建立檔案
            readContent = [NSString stringWithContentsOfFile:createPath encoding:NSUTF8StringEncoding error:&ReadFileError;];           
        }
    }
    else
    {
        if (nil == writeContent) {
            return nil;
        }else{
            data = [writeContent dataUsingEncoding:NSUTF8StringEncoding];//新檔案的初始資料
            [[NSFileManager defaultManager] createFileAtPath:createPath contents:data attributes:nil];//建立檔案
            readContent = [NSString stringWithContentsOfFile:createPath encoding:NSUTF8StringEncoding error:&ReadFileError;];
        }
    }
    return readContent;
}

( NSArray * )storeArryFile:(NSString *)fileName content:( NSArray *)writeArry
{
    //步驟
    //存 :將 陣列放入 字典
    //取: 從字典取出陣列
    if ( writeArry == nil)//讀取檔案
    {
        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;
}

您可能感興趣的文章:

Objective-C中使用NSString類操作字串的方法小結NSString與C字串之間的相互轉換Objective-C中字串的拼接方法小結Objective-C處理空字串和頁面傳值及自定義拷貝IOS開發之路–C語言陣列和字串Objective-C中字串NSString的常用操作方法總結

相關文章

IOS開發 最新文章