2016-12-13 4 views
0

私のバンドルのリソースからアプリケーションサポートディレクトリにSQLiteファイルをプリロードする必要があります。コアデータがデフォルトでそこに置く空ファイルに対して、正しいファイルが存在するかどうか確認したい。これを行うには、FileManager.default.contentsEqualを使用しています。ただし、これは常にfalseを返します。コピーしたファイルを比較すると、FileManager.contentsEqualがfalseを返します

私は遊び場でテストしようとしましたが、そこにはエイリアスファイルを作成していますが、まだfalseの比較が行われています。

アプリでは、ファイルは同じ名前とサイズでコピーされます。日付は異なります。コピーには元のタイムスタンプではなく現在の日付/時刻が入ります。しかし、contentsEqualを使って、私はそれが重要だとは思わないでしょう。

更新:コマンドラインでdiffは、ファイルが同じである示しています...

私は何をしないのですか?

// get the URL for the application support directory 
let appSupportDir: URL = try! 
FileManager.default.url(for: FileManager.SearchPathDirectory.applicationSupportDirectory, 
         in: FileManager.SearchPathDomainMask.userDomainMask, 
         appropriateFor: nil, create: true) 

// get the source URLs for the preload files 
let sqliteFileBundleURL: URL = Bundle.main.url(forResource: "My_DB", withExtension: "sqlite")! 
let sqliteShmFileBundleURL: URL = Bundle.main.url(forResource: "My_DB", withExtension: "sqlite-shm")! 
let sqliteWalFileBundleURL: URL = Bundle.main.url(forResource: "My_DB", withExtension: "sqlite-wal")! 

// create target URLs for copy to application support directory 
let sqliteFileAppSptURL: URL = appSupportDir.appendingPathComponent("My_DB.sqlite") 
let sqliteShmFileAppSptURL: URL = appSupportDir.appendingPathComponent("My_DB.sqlite-shm") 
let sqliteWalFileAppSptURL: URL = appSupportDir.appendingPathComponent("My_DB.sqlite-wal") 

// remove the files if they already exist at the target (for test - app doesn't do this) 
do { 
    let filesFound: [URL] = try FileManager.default.contentsOfDirectory(at: appSupportDir, 
                    includingPropertiesForKeys: nil, 
                    options: .skipsHiddenFiles) 
    if !filesFound.isEmpty { 
     for fileURL in filesFound { 
      try FileManager.default.removeItem(at: fileURL) 
     } 
     print("Removed \(filesFound.count) files without error.") 
    } 
} 
catch { 
    print("Error:\n\(error)") 
} 

// copy the files to the application support directory 
do { 
    try FileManager.default.copyItem(at: sqliteFileBundleURL, to: sqliteFileAppSptURL) 
    try FileManager.default.copyItem(at: sqliteShmFileBundleURL, to: sqliteShmFileAppSptURL) 
    try FileManager.default.copyItem(at: sqliteWalFileBundleURL, to: sqliteWalFileAppSptURL) 
} 
catch { 
    print("Error: \(error)") 
} 

// compare the copied target files to their source using contentsEqual 
let sqliteFileCopied: Bool = 
FileManager.default.contentsEqual(atPath: sqliteFileBundleURL.absoluteString, andPath: sqliteFileAppSptURL.absoluteString) 
let sqliteShmFileCopied: Bool = 
FileManager.default.contentsEqual(atPath: sqliteShmFileBundleURL.absoluteString, andPath: sqliteShmFileAppSptURL.absoluteString) 
let sqliteWalFileCopied: Bool = 
FileManager.default.contentsEqual(atPath: sqliteWalFileBundleURL.absoluteString, andPath: sqliteWalFileAppSptURL.absoluteString) 

答えて

1

なるほど:


ここでは、実質私のアプリのコードと同じである遊び場からのコードは、です! FileManagerを使用する場合、一方が変換するpathなくabsoluteStringを使用しなければならないURLStringに:

/var/folders/kb/y2d_vrl133d1b04_5kc3kw880000gn/T/com.apple.dt.Xcode.pg/resources/238FF955-236A-42FC-B6EA-9A74FC52F235/My_DB.sqlite 

// compare the copied target files to their source using contentsEqual 
let sqliteFileCopied: Bool = 
FileManager.default.contentsEqual(atPath: sqliteFileBundleURL.path, andPath: sqliteFileAppSptURL.path) 
let sqliteShmFileCopied: Bool = 
FileManager.default.contentsEqual(atPath: sqliteShmFileBundleURL.path, andPath: sqliteShmFileAppSptURL.path) 
let sqliteWalFileCopied: Bool = 
FileManager.default.contentsEqual(atPath: sqliteWalFileBundleURL.path, andPath: sqliteWalFileAppSptURL.path) 

2つの違いはpathは、ファイルシステムタイプのパスを生成することです

absoluteStringは、ブラウザに適したパスを生成します。

file:///var/folders/kb/y2d_vrl133d1b04_5kc3kw880000gn/T/com.apple.dt.Xcode.pg/resources/238FF955-236A-42FC-B6EA-9A74FC52F235/My_DB.sqlite 

pathも、エイリアスファイルと遊び場で動作します。

関連する問題