你好,歡迎來到IOS教程網

 Ios教程網 >> IOS教程 >> 關於IOS教程 >> Objective-C封裝字符串存儲操作示例

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

編輯:關於IOS教程

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

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved