2016-12-13 21 views
0

我需要將SQLite文件從我的包資源中預加載到應用程序支持目錄中。我想確保正確的文件與Core Data默認放置的空文件相對應。爲此,我使用FileManager.default.contentsEqual;但是,這始終返回falseFileManager.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

// 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) 

這兩者之間的區別是,path生成一個文件系統類型的路徑:

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

absoluteString生成瀏覽器友好路徑:

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

path還與別名文件的遊樂場。

相關問題