2017-06-20 58 views
7

我保存在一个文件目录下的文件中迅速3验证码:将文件保存在swift 3文件目录中?

fileManager = FileManager.default 
// let documentDirectory = fileManager?.urls(for: .documentDirectory, in: .userDomainMask).first as String 
var path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String 
path = path + name 

let image = #imageLiteral(resourceName: "Notifications") 
let imageData = UIImageJPEGRepresentation(image, 0.5) 
let bool = fileManager?.createFile(atPath: path, contents: imageData, attributes: nil) 

print("bool is \(bool)") 
return true 

但正如你所看到的,我没有使用filemanager获取文档目录路径filemanager只给出网址不串。

问题:

  • 如何从文件管理器串?
  • 有没有在我的代码崩溃的机会?
+0

'让文件管理器= FileManager.default.urls(为:.documentDirectory在:.userDomainMask)。首先? .path' – WeiJay

回答

22

请想一想。

URL是处理文件路径的推荐方式,因为它包含所有便捷方法来添加和删除路径组件和扩展 - 而不是String Apple从中移除了这些方法。

您不鼓励连接像path = path + name这样的路径。这很容易出错,因为您负责所有斜线路径分隔符。

此外,您不需要使用FileManager创建文件。 Data有一个将数据写入磁盘的方法。

let fileManager = FileManager.default 
do { 
    let documentDirectory = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false) 
    let fileURL = documentDirectory.appendingPathComponent(name) 
    let image = #imageLiteral(resourceName: "Notifications") 
    if let imageData = UIImageJPEGRepresentation(image, 0.5) { 
     try imageData.write(to: fileURL) 
     return true 
    } 
} catch { 
    print(error) 
} 
return false 
-5

FUNC copyandpast(){

var path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true); 
    let dbpath :NSString = path[0] as NSString; 

    let strdbpath = dbpath.strings(byAppendingPaths: ["mydb.db"])[0] ; 
    print(strdbpath); 
    let fmnager = FileManager.default; 

    if !fmnager.fileExists(atPath: strdbpath) { 

     let local = Bundle.main.path(forResource: "mydb", ofType: "db"); 

     do 
     { 
      try fmnager.copyItem(atPath: local!, toPath: strdbpath) 

     }catch{ 

     } 



    } 


} 
2

继vadian您需要保存(数据)文件中的文件目录中唯一的一行给出上面的例子是:

试imageData.write(to:fileURL)

获取文件路径是有趣的部分

例如:创建文件路径

func createNewDirPath()->URL{ 

let dirPathNoScheme = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String 

    //add directory path file Scheme; some operations fail w/out it 
    let dirPath = "file://\(dirPathNoScheme)" 
    //name your file, make sure you get the ext right .mp3/.wav/.m4a/.mov/.whatever 
    let fileName = "thisIsYourFileName.mov" 
    let pathArray = [dirPath, fileName] 
    let path = URL(string: pathArray.joined(separator: "/")) 

    //use a guard since the result is an optional 
    guard let filePath = path else { 
     //if it fails do this stuff: 
     return URL(string: "choose how to handle error here")! 
    } 
    //if it works return the filePath 
    return filePath 
} 

调用该函数:

let shinnyNewURLpath = createNewDirPath() 


//write data to file using one line in do/catch operation 
do { 
    try yourDataObject?.write(to: shinnyNewURLpath) 
    } 
catch { 
     print("catch error",error.localizedDescription) 
}