LoginSignup
0
0

More than 3 years have passed since last update.

SwiftFilePathをアプリから削除する

Posted at

SwiftFilePathというライブラリを使用しているが、更新が滞っておりSwift5のマイグレーションの支障となっているため、削除しよと思う。
削除のための備忘録を残す。

SwiftFilePathって

Swiftないで保存したファイルなどへのアクセスなどを簡単にするためのライブラリぽい。
https://qiita.com/nori0620/items/80b5251b1127a71c6bd8

この作者の人が作成したようですが、2015年から更新が止まっています。

Path.documentsDirを置き換える

Util.swift
        let path = Path.documentsDir["ReadGirl"]
        if path.exists { return }
        path.mkdir()

この部分のPath.documentDir["ReadFirl"]という部分と、path.exsts, path.mkdirを変更します。

Util.swift
        let fileManager = FileManager.default
        let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
        let filePath  = documentsPath.appending("/ReadGirl")
        let isExists: Bool = fileManager.fileExists(atPath: filePath)
        if isExists { return }
        do {
            try fileManager.createDirectory(atPath: filePath, withIntermediateDirectories: true, attributes: nil)
        } catch {
            print("ERROR: \(error.localizedDescription)")
        }

確かに、SwiftFilePathを無くすと処理が多くなる。

.path_stringが付いている場合

NSoundPalyService.swift
  let path = Path.documentsDir["ReadGirl"][title].path_string

こいつを

NSoundPlayService.swift
            let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
            let path  = documentsPath.appending("/ReadGirl/\(title)")

path.existsの変更

最初の方にもありました。existsはファイルが存在するかどうかを判定しているようです。

AudioDownloadService.swift
    func isDownloaded(title: String) -> Bool {
        if isDownloading(title: title) { return false }
        let path = Path.documentsDir["ReadGirl"][title]
        return path.exists
    }

これを

AudioDwonloadService.swif
    func isDownloaded(title: String) -> Bool {
        if isDownloading(title: title) { return false }
        let fileManager = FileManager.default
        let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
        let filePath  = documentsPath.appending("/ReadGirl/\(title)")
        let isExists: Bool = fileManager.fileExists(atPath: filePath)
        return isExists
    }

deleten(.remove)の変更

AudioDownloadService.swift
    func delete(by title: String) {
        let path = Path.documentsDir["ReadGirl"][title]
        if !path.exists { return }
        path.remove()
    }

こいつを

AudioDownloadService.swift
    func delete(by title: String) {
        let fileManager = FileManager.default
        let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
        let filePath  = documentsPath.appending("/ReadGirl/\(title)")
        let isExists: Bool = fileManager.fileExists(atPath: filePath)
        if !isExists { return }
        try? fileManager.removeItem(atPath: filePath)
    }

writeHTMLの変更

これはHTMLをファイルに書き込む作業。

Book.swift
    func writeHTML() {
        if htmlPath.exists { return }
        guard let data = description.data(using: .shiftJIS) else { return }
        htmlPath.writeData(data)
    }

htmlファイルが存在するかどうかを判定して、あれば、.shiftJISで書き込むってやつ

Book.swift
    func writeHTML() {
        let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
        let filePath  = documentsPath.appending("/ReadGirl/\(id).html")
        guard let data = description.data(using: .shiftJIS) else { return }
        try? data.write(to: URL(string: filePath)!)
    }

まとめ

ライブラリを利用するのは初期の開発では非常に効率が良いが、運用を考えると乱立して使用することは非常に愚行と言える。簡単な処理であればめんどくさがらず独自で作った方が読み込みも早いし、メンテナンスもしやすい。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0