2017-06-20 8 views
7

ファイルを迅速にドキュメントディレクトリに保存しますか?私はこのコードで迅速な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はURLのみではない文字列を与えると、ドキュメントディレクトリのパスを取得するにはfilemanagerを使用していません。

質問:

  • ファイルマネージャから文字列を取得する方法は?
  • コードにクラッシュする可能性はありますか?
+0

'聞かせてファイルマネージャ= FileManager.default.urls(用:.documentDirectory、中:.userDomainMask)1次回? .path' – WeiJay

答えて

22

他の方法で考えてください。

URLは、Appleがこれらのメソッドを削除したStringではなく、パスコンポーネントと拡張子を追加および削除する便利な方法をすべて含んでいるため、ファイルパスを処理するための推奨方法です。

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

は次のとおりです。

試してみます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) 
} 
関連する問題