OSDN Git Service

staticプロパティをインスタンスプロパティに変更
[kcd/KCD.git] / KCD / ApplicationDirecrories.swift
index acdf279..ce1f60a 100644 (file)
@@ -31,14 +31,16 @@ func localizedAppName() -> String {
 
 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
@@ -48,73 +50,44 @@ struct ApplicationDirecrories {
 
 extension ApplicationDirecrories {
     
-    static let screenshotSaveDirectoryURL: URL = {
-        
-        let parentURL = URL(fileURLWithPath: AppDelegate.shared.screenShotSaveDirectory)
-        let url = parentURL.appendingPathComponent(localizedAppName())
-        let fm = FileManager.default
-        var isDir: ObjCBool = false
+    func createDirectory(_ url: URL) -> Bool {
         
         do {
             
-            if !fm.fileExists(atPath: url.path, isDirectory: &isDir) {
-                
-                try fm.createDirectory(at: url, withIntermediateDirectories: false)
-                
-            } else if !isDir.boolValue {
-                
-                print("\(url) is regular file, not direcory.")
-                return parentURL
-            }
+            try FileManager.default.createDirectory(at: url,
+                                                    withIntermediateDirectories: false,
+                                                    attributes: nil)
+            
+            return true
             
         } catch {
             
-            print("Can not create screenshot save directory.")
-            return parentURL
+            return false
         }
-        
-        return url
-    }()
-}
-
-
-func createDirectory(_ url: URL) -> Bool {
-    
-    do {
-        
-        try FileManager.default.createDirectory(at: url,
-                                                withIntermediateDirectories: false,
-                                                attributes: nil)
-        
-        return true
-        
-    } catch {
-        
-        return false
     }
-}
-
-func checkDirectory(_ url: URL, create: Bool) -> Bool {
     
-    do {
+    func checkDirectory(_ url: URL, create: Bool) -> Bool {
         
-        let resourceValue = try url.resourceValues(forKeys: [.isDirectoryKey])
-        if !resourceValue.isDirectory! {
+        do {
             
-            print("Expected a folder to store application data, found a file \(url.path).")
+            let resourceValue = try url.resourceValues(forKeys: [.isDirectoryKey])
+            if !resourceValue.isDirectory! {
+                
+                print("Expected a folder to store application data, found a file \(url.path).")
+                
+                return false
+            }
             
-            return false
-        }
-        
-        return true
-        
-    } catch let error as NSError {
-        
-        if create, error.code == NSFileReadNoSuchFileError {
+            return true
+            
+        } catch let error as NSError {
+            
+            if create, error.code == NSFileReadNoSuchFileError {
+                
+                return createDirectory(url)
+            }
             
-            return createDirectory(url)
+            return false
         }
-        
-        return false
     }
 }