Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.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
Ios 将文件创建到应用程序文档文件夹_Ios_Objective C_Xcode - Fatal编程技术网

Ios 将文件创建到应用程序文档文件夹

Ios 将文件创建到应用程序文档文件夹,ios,objective-c,xcode,Ios,Objective C,Xcode,我想在我的应用程序文档文件夹中创建一个文件,我必须存储一些内容。该内容取自我的本地主机文件。所以我写了下面的代码。但是这段代码没有在我的应用程序文档文件夹中创建文件 - (void) createFile { NSString *strPath = [NSString stringWithFormat:@"http://192.168.5.117/~mac/banana.obj"]; NSData *data =[NSData dataWithCo

我想在我的应用程序文档文件夹中创建一个文件,我必须存储一些内容。该内容取自我的本地主机文件。所以我写了下面的代码。但是这段代码没有在我的应用程序文档文件夹中创建文件

   - (void) createFile
    {
        NSString *strPath = [NSString stringWithFormat:@"http://192.168.5.117/~mac/banana.obj"];

        NSData *data =[NSData dataWithContentsOfURL:[NSURL URLWithString:strPath]];

        NSString *strLastpath = [strPath lastPathComponent];

        NSString *folderName = [@"Object File/" stringByAppendingString:strLastpath];

        NSLog(@"Path : %@ \n File Name : %@",strPath,folderName);

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

        NSString *docsDir = [dirPaths objectAtIndex:0];

        databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:folderName]];

        NSLog(@"Database Path : %@",databasePath);

        NSFileManager *filemgr = [NSFileManager defaultManager];

        if ([filemgr fileExistsAtPath:databasePath] == YES)
        {
            NSLog(@"File Exists");
        }
        else
        {
            NSLog(@"File not exists");
        }

        NSString *content = [NSString stringWithFormat:@"%@",data];

        [content writeToFile:strLastpath atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];

    }
在为跑步而构建时,我得到了这个

2013-10-11 11:36:38.833 SampleFileCreation[1321:c07] Path : http://192.168.5.117/~mac/banana.obj 

File Name : Object File/banana.obj

2013-10-11 11:36:38.834 SampleFileCreation[1321:c07] Database Path : /Users/mac/Library/Application Support/iPhone Simulator/6.1/Applications/4FA749DF-D12D-4956-AF76-140D2F981F17/Documents/Object File/banana.obj

2013-10-11 11:36:38.834 SampleFileCreation[1321:c07] File not exists

我找到了答案。文件已成功创建

- (void) createFile {

    NSString *strPath = [NSString stringWithFormat:@"http://-.-.-.-/~mac/banana.obj"];        
    NSData *data =[NSData dataWithContentsOfURL:[NSURL URLWithString:strPath]];            
    NSString *strLastpath = [strPath lastPathComponent];        
    NSString *folderName = [@"Object File/" stringByAppendingString:strLastpath];        
    NSLog(@"Path : %@ \n File Name : %@",strPath,folderName);        
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                       NSUserDomainMask, YES);

    NSString *docsDir = [dirPaths objectAtIndex:0];        
    docsDir=[docsDir stringByAppendingString:@"/Object File"];        
    databasePath = [[NSString alloc] initWithString: 
                    [docsDir stringByAppendingPathComponent:folderName]];

    NSLog(@"Database Path : %@",databasePath);        
    NSError *theError = nil;       
    NSFileManager *filemgr = [NSFileManager defaultManager];

    **//I added this line**
    [filemgr createDirectoryAtPath:docsDir 
       withIntermediateDirectories:YES 
                        attributes:nil 
                             error:&theError];

    **//Changed this line**
    [data writeToFile:[docsDir stringByAppendingPathComponent:strLastpath]            
              options:NSDataWritingAtomic  
                error:&theError];

    if ([filemgr fileExistsAtPath:[docsDir stringByAppendingPathComponent:strLastpath]]){
        NSLog(@"File Exists");
    } else {
        NSLog(@"File not exists");
        NSLog(@"Tell Me error %@",[theError localizedDescription]);
    }       
}

您也可以尝试此代码

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *fileName = @"yourFileName";
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/fileName"];

NSDate *data = [NSData dataWithContentsOfURL:YourUrl]; // get date form url
[data writeToFile:dataPath atomically:YES]; // save data in file

您确定NSData*data=[NSData data WITH CONTENTSOFURL:[NSURL URLWithString:strPath]];这是给你下载的数据吗?是的,我得到了所有的内容,我用NSLog检查过。你要下载哪种文件类型??@iAmbitious:Wavefront obj文件。文件扩展名为“.obj”n错误*错误=nil;[内容写入文件:strLastpath原子:无编码:nsStringEncodingConversionLowlossy错误:&错误];如果(错误){NSLog(@“告诉我错误%@”,[theError localizedDescription]);}将给定的代码行用NSError结束,并告诉调试器所说的内容。