OSDN Git Service

staticプロパティをインスタンスプロパティに変更
[kcd/KCD.git] / KCD / ApplicationDirecrories.swift
index c0f8ea7..ce1f60a 100644 (file)
@@ -18,16 +18,29 @@ private func supportDirName() -> String {
         ?? "UnknownAppliation"
 }
 
+func localizedAppName() -> String {
+    
+    guard let name = Bundle.main.localizedInfoDictionary?["CFBundleName"] as? String,
+        !name.isEmpty else {
+            
+            return supportDirName()
+    }
+    
+    return name
+}
+
 struct ApplicationDirecrories {
     
-    static let support = searchedURL(for: .applicationSupportDirectory)
+    static let shared = ApplicationDirecrories()
+    
+    let support = searchedURL(for: .applicationSupportDirectory)
         .appendingPathComponent(supportDirName())
     
-    static let documents = searchedURL(for: .documentDirectory)
+    let documents = searchedURL(for: .documentDirectory)
     
-    static let pictures = searchedURL(for: .picturesDirectory)
+    let pictures = searchedURL(for: .picturesDirectory)
     
-    private static func searchedURL(for directory: FileManager.SearchPathDirectory) -> URL {
+    static func searchedURL(for directory: FileManager.SearchPathDirectory) -> URL {
         
         return FileManager.default.urls(for: directory, in: .userDomainMask)
             .last
@@ -35,41 +48,46 @@ struct ApplicationDirecrories {
     }
 }
 
-func checkDirectory(_ url: URL) -> Bool {
-    
-    var success = true
+extension ApplicationDirecrories {
     
-    do {
+    func createDirectory(_ url: URL) -> Bool {
         
-        let p = try url.resourceValues(forKeys: [.isDirectoryKey])
-        if !p.isDirectory! {
+        do {
+            
+            try FileManager.default.createDirectory(at: url,
+                                                    withIntermediateDirectories: false,
+                                                    attributes: nil)
+            
+            return true
             
-            print("Expected a folder to store application data, found a file \(url.path).")
-            success = false
+        } catch {
+            
+            return false
         }
+    }
+    
+    func checkDirectory(_ url: URL, create: Bool) -> Bool {
         
-    } catch {
-        
-        let nserror = error as NSError
-        if nserror.code == NSFileReadNoSuchFileError {
+        do {
             
-            do {
+            let resourceValue = try url.resourceValues(forKeys: [.isDirectoryKey])
+            if !resourceValue.isDirectory! {
                 
-                try FileManager
-                    .default
-                    .createDirectory(at: url,
-                                     withIntermediateDirectories: false,
-                                     attributes: nil)
+                print("Expected a folder to store application data, found a file \(url.path).")
                 
-            } catch {
+                return false
+            }
+            
+            return true
+            
+        } catch let error as NSError {
+            
+            if create, error.code == NSFileReadNoSuchFileError {
                 
-                success = false
+                return createDirectory(url)
             }
-        } else {
             
-            success = false
+            return false
         }
     }
-    
-    return success
 }