在ios中的文档目录中写入文件

时间:2016-07-29 09:16:43

标签: ios iphone swift swift2

嗨我想在Document目录中写一些音频文件,起初我用下面的代码创建了文件夹,文件夹创建正确..

func createfolder(throwerror: AnyObject)  {

    let documentsDirectory: AnyObject = Directorypath[0]
    let finalPath = documentsDirectory.stringByAppendingPathComponent("Audio")
    if !NSFileManager.defaultManager().fileExistsAtPath(finalPath, isDirectory: &isDirectory) {
        do {
    try NSFileManager.defaultManager().createDirectoryAtPath(finalPath, withIntermediateDirectories: false, attributes: nil);
        } catch let error as NSError {
            print("error.localizedDescription",error.localizedDescription);
            exceptionobj.filedirectoryexecption(.isfoldercreated, controller: throwerror as! UIViewController)
        }
    }else{print("Folder Exist");}
}

然后我想使用下面的代码

写下音频文件
func WritefileintoDirectory(viewcontroller: AnyObject,filedata:NSData?,filetype :Filetype,filename : String,Rootfolder : Filetype){

        var tempfilename = "/";
        tempfilename += filename  
        switch filetype {
        case .Image:
            tempfilename += ".png"
        case .Audio:
            tempfilename += ".m4a"
        }
        let documentsDirectory: AnyObject = Directorypath[0]
        let directorypath = documentsDirectory.stringByAppendingPathComponent(String(Rootfolder));
        let finalPath = directorypath + tempfilename;
        print("finalPath",finalPath);
        if filedata != nil{
            let Status =  NSFileManager.defaultManager().createFileAtPath(finalPath, contents: filedata, attributes: nil);
            if !Status {
              exceptionobj.filedirectoryexecption(.isfilewritten, controller: viewcontroller as! UIViewController)
            }
        }
    }

上面的代码,我将通过文件名等" httpstoragegoogleapiscommememtobackend1321appspotcomaudiosL2FwcGhvc3RpbmdfZXVyb3BlL2Jsb2JzL0FFbkIyVXBjNlgtbUk5aGxzcWQzTjg2Rmw5bmlQd05vbXRzYzdzaVJUN1RUTFV4UlFHcUhKR1V0UHJNR0k3cjZpOURPTlZ4d0s4V3ZEbFVrbFlicDQ0WUhaanA2d0s1T0tMNmFKOWJVUnNvY3BIenFCZDNlb0VrLk96N0RYd2pVcE5oTnhRSHg"但该文件无法写入目录。如果我硬编码文件名,如" audiofile1"它工作得很好..

请告诉我这里的问题是什么......

0 个答案:

没有答案