Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c NSFileManager创建文件失败 NSArray*path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString*documentsDirectory=[paths objectAtIndex:0]; NSString*文件路径; 如果(str_FolderName!=nil){ 文件路径=[DocumentsDirectoryStringByAppendingPathComponent:[NSString stringWithFormat:@/%@/%@],str_FolderName,str_FileName]; }否则{ filePath=[DocumentsDirectoryStringByAppendingPathComponent:[NSString stringWithFormat:@/%@],str_FileName]]; } NSFileManager*fileManager=[NSFileManager defaultManager]; NSMutableDictionary*数据; 如果([fileManager fileExistsAtPath:filePath]) { //文件不存在 数据=[[NSMutableDictionary alloc]initWithContentsOfFile:filePath]; }否则{ BOOL Success=[fileManager createFileAtPath:filePath内容:nil属性:nil]; //有时返回是,有时返回否_Objective C - Fatal编程技术网

Objective c NSFileManager创建文件失败 NSArray*path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString*documentsDirectory=[paths objectAtIndex:0]; NSString*文件路径; 如果(str_FolderName!=nil){ 文件路径=[DocumentsDirectoryStringByAppendingPathComponent:[NSString stringWithFormat:@/%@/%@],str_FolderName,str_FileName]; }否则{ filePath=[DocumentsDirectoryStringByAppendingPathComponent:[NSString stringWithFormat:@/%@],str_FileName]]; } NSFileManager*fileManager=[NSFileManager defaultManager]; NSMutableDictionary*数据; 如果([fileManager fileExistsAtPath:filePath]) { //文件不存在 数据=[[NSMutableDictionary alloc]initWithContentsOfFile:filePath]; }否则{ BOOL Success=[fileManager createFileAtPath:filePath内容:nil属性:nil]; //有时返回是,有时返回否

Objective c NSFileManager创建文件失败 NSArray*path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString*documentsDirectory=[paths objectAtIndex:0]; NSString*文件路径; 如果(str_FolderName!=nil){ 文件路径=[DocumentsDirectoryStringByAppendingPathComponent:[NSString stringWithFormat:@/%@/%@],str_FolderName,str_FileName]; }否则{ filePath=[DocumentsDirectoryStringByAppendingPathComponent:[NSString stringWithFormat:@/%@],str_FileName]]; } NSFileManager*fileManager=[NSFileManager defaultManager]; NSMutableDictionary*数据; 如果([fileManager fileExistsAtPath:filePath]) { //文件不存在 数据=[[NSMutableDictionary alloc]initWithContentsOfFile:filePath]; }否则{ BOOL Success=[fileManager createFileAtPath:filePath内容:nil属性:nil]; //有时返回是,有时返回否,objective-c,Objective C,我更新了您的一些代码。我将NSFilemanager的正确分配及其对我的工作进行了说明。您可以在文件路径或filemanager对象中遇到问题 NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath; if

我更新了您的一些代码。我将NSFilemanager的正确分配及其对我的工作进行了说明。您可以在文件路径或filemanager对象中遇到问题

NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *filePath;

if (str_FolderName!=nil){
    filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@/%@",str_FolderName,str_FileName]];
} else {
    filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",str_FileName]];
}

NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableDictionary *data;

if ([fileManager fileExistsAtPath: filePath])
{
    //file is exist
    data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];

} else {
    BOOL Success = [fileManager createFileAtPath:filePath contents:nil attributes:nil];
    // sometime return YES, sometime return NO <<< this is my question
    if(Success == YES){
        data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
    }else{ 
        data = [[NSMutableDictionary alloc] init];            
    }
}

for (uint32_t u32_Cnt=0;u32_Cnt<arr_Keys.count;u32_Cnt++) {
    [data setValue:[NSString stringWithFormat:@"%@",[marr_Data objectAtIndex:u32_Cnt]] forKey:[arr_Keys objectAtIndex:u32_Cnt]];
}

if ([data writeToFile:filePath atomically: YES])
{
    NSLog(@"[Save OK]");

    return DEF_PASS;
} else {
    NSLog(@"[Save Fail]");  <<<<   now happened
}

不同的是我的路径包含一个文件夹名,然后我测试它是否成功创建了没有文件夹的文件,但是创建了一个文件夹的文件失败了…你能给它一个吗?filePath=[DocumentsDirectoryStringByAppendingPathComponent:[NSString stringWithFormat:@/%@],str_FolderName,str_FileName]];您得到的答案是错误的,因为当您尝试同时使用文件夹和文件名创建时,您还必须创建字典。让我更新代码。您的意思是我不能使用dolder创建文件?好的……我读了您的更新
 NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath;

    if (str_FolderName!=nil){
        filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@/%@",str_FolderName,str_FileName]];
    }else{
        filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",str_FileName]];
    }
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSMutableDictionary *data;
    if ([fileManager fileExistsAtPath: filePath])
    {
        //file is exist
        data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];

    }
    else
    {
        if (str_FolderName!=nil){
            NSString *strDicPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",str_FolderName]];
            // You need to create director before creating file.
            if (![[NSFileManager defaultManager] fileExistsAtPath:strDicPath])
            {
                NSError *error;
                [[NSFileManager defaultManager] createDirectoryAtPath:strDicPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
            }

        }
        BOOL Success = [fileManager createFileAtPath:filePath contents:nil attributes:nil];
        // sometime return YES, sometime return NO <<< this is my question
        if(Success == YES){
            data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
        }else{ 
            data = [[NSMutableDictionary alloc] init];            
        }
    }
NSError *error;
NSString *stringToWrite = @"1\n2\n3\n4";
[stringToWrite writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];