2017-01-13 5 views
0

私はこれを作成しましたFileManagerextensionこのextensionで、私はそうのようなファイル階層を作成したい:iOS - FileManager拡張のベストプラクティス

  • アプリケーションサポート
    • お気に入り
    • フィード
      • 画像

これは、FileManagerextensionのコードです。これは、アプリケーションが起動するとすぐにapp delegateに呼び出されます。次に、このコードを使用して、常にpathのフォルダを取得します。

これは、この階層を作成し、必要なときにパスを取得するための良い方法ですか?これはいい練習ですか?

extension FileManager { 
    static func createOrFindApplicationDirectory() -> URL? { 
     let bundleID = Bundle.main.bundleIdentifier 
     // Find the application support directory in the home directory. 
     let appSupportDir = self.default.urls(for: .applicationSupportDirectory, in: .userDomainMask) 

     guard appSupportDir.count > 0 else { 
      return nil 
     } 

     // Append the bundle ID to the URL for the Application Support directory. 
     let dirPath = appSupportDir[0].appendingPathComponent(bundleID!) 

     // If the directory does not exist, this method creates it. 
     do { 
      try self.default.createDirectory(at: dirPath, withIntermediateDirectories: true, attributes: nil) 
      return dirPath 
     } catch let error { 
      print("Error creating Application Support directory with error: \(error)") 
      return nil 
     } 
    } 

    static func createOrFindFavoritesDirectory() -> URL? { 
     guard let appSupportDir = createOrFindApplicationDirectory() else { 
      return nil 
     } 

     let dirPath = appSupportDir.appendingPathComponent("Favorites") 

     // If the directory does not exist, this method creates it. 
     do { 
      try self.default.createDirectory(at: dirPath, withIntermediateDirectories: true, attributes: nil) 
      return dirPath 
     } catch let error { 
      print("Error creating Favorites directory with error: \(error)") 
      return nil 
     } 
    } 

    static func createOrFindFeedDirectory() -> URL? { 
     guard let appSupportDir = createOrFindFavoritesDirectory() else { 
      return nil 
     } 

     let dirPath = appSupportDir.appendingPathComponent("Feed") 

     // If the directory does not exist, this method creates it. 
     do { 
      try self.default.createDirectory(at: dirPath, withIntermediateDirectories: true, attributes: nil) 
      return dirPath 
     } catch let error { 
      print("Error creating Favorites directory with error: \(error)") 
      return nil 
     } 
    } 

    static func currentImagesDirectory() -> URL? { 
     guard let feedDir = createOrFindFeedDirectory() else { 
      return nil 
     } 

     let dirPath = feedDir.appendingPathComponent("Images") 

     // If the directory does not exist, this method creates it. 
     do { 
      try self.default.createDirectory(at: dirPath, withIntermediateDirectories: true, attributes: nil) 
      return dirPath 
     } catch let error { 
      print("Error creating Images directory with error: \(error)") 
      return nil 
     } 
    } 
} 
+0

は私には正常に見えます。 –

+0

@ILikeTauありがとう!あなたは何か違うことはありますか?自分のコードやチュートリアル/リンクなど、私がやろうとしているものに似た何か他の例を見たいと思っています。 – JEL

+0

私は、 'createOrFindFavoritesDirectory()'と 'createOrFindFeedDirectory()'を引数を取る一つの関数に組み合わせることができますが、それ以外はすべてうまく見えます。 –

答えて

1

それはかなり良いように見えますが、あなたはコードのビットを組み合わせて、より良いエラーチェックを持つことができます:

extension FileManager { 
    static func createOrFindApplicationDirectory() -> URL? { 
     guard let bundleID = Bundle.main.bundleIdentifier else { 
      return nil 
     } 

     // Find the application support directory in the home directory. 
     let appSupportDirArray = self.default.urls(for: .applicationSupportDirectory, in: .userDomainMask) 

     guard let appSupportDir = appSupportDirArray.first else { 
      return nil 
     } 

     // Append the bundle ID to the URL for the Application Support directory. 
     let dirPath = appSupportDir.appendingPathComponent(bundleID) 

     // If the directory does not exist, this method creates it. 
     do { 
      try self.default.createDirectory(at: dirPath, withIntermediateDirectories: true, attributes: nil) 
      return dirPath 
     } catch let error { 
      print("Error creating Application Support directory with error: \(error)") 
      return nil 
     } 
    } 

    static func createOrFindDirectory(named name: String) -> URL? { 
     guard let appSupportDir = createOrFindApplicationDirectory() else { 
      return nil 
     } 

     let dirPath = appSupportDir.appendingPathComponent(name) 

     // If the directory does not exist, this method creates it. 
     do { 
      try self.default.createDirectory(at: dirPath, withIntermediateDirectories: true, attributes: nil) 
      return dirPath 
     } catch let error { 
      print("Error creating \(name) directory with error: \(error)") 
      return nil 
     } 
    } 

    static func currentImagesDirectory() -> URL? { 
     guard let feedDir = createOrFindDirectory(named: "Feed") else { 
      return nil 
     } 

     let dirPath = feedDir.appendingPathComponent("Images") 

     // If the directory does not exist, this method creates it. 
     do { 
      try self.default.createDirectory(at: dirPath, withIntermediateDirectories: true, attributes: nil) 
      return dirPath 
     } catch let error { 
      print("Error creating Images directory with error: \(error)") 
      return nil 
     } 
    } 
} 
+0

いいね!フィードとお気に入りを1つの関数 'static func createOrFindDirectory(名前:String) - > URL? 'にまとめたことが分かります。 'エラーチェック'の面では、アプリケーションが最初に起動されたときに作成されるこれらのフォルダ/ディレクトリが本当に必要です。あなたは 'nil'を返す以外にエラーを処理して、これらのフォルダが進行する前に作成されることを保証しますか?ポイントは、アプリケーションの起動時にこれらのフォルダが作成されていないと、アプリケーションが正常に動作しない – JEL

+0

エラー処理について言及して以来、もっと考えています。そのような場合は、処理する方法がわからないため、アプリケーションが作成されるまで進まない – JEL

関連する問題