OSDN Git Service

staticプロパティをインスタンスプロパティに変更
[kcd/KCD.git] / KCD / ApplicationDirecrories.swift
index e60a87a..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
@@ -46,44 +48,46 @@ struct ApplicationDirecrories {
     }
 }
 
-
-func createDirectory(_ url: URL) -> Bool {
+extension ApplicationDirecrories {
     
-    do {
-        
-        try FileManager.default.createDirectory(at: url,
-                                                withIntermediateDirectories: false,
-                                                attributes: nil)
+    func createDirectory(_ url: URL) -> Bool {
         
-        return true
-        
-    } catch {
-        
-        return false
-    }
-}
-
-func checkDirectory(_ url: URL, create: Bool) -> Bool {
-    
-    do {
-        
-        let resourceValue = try url.resourceValues(forKeys: [.isDirectoryKey])
-        if !resourceValue.isDirectory! {
+        do {
+            
+            try FileManager.default.createDirectory(at: url,
+                                                    withIntermediateDirectories: false,
+                                                    attributes: nil)
             
-            print("Expected a folder to store application data, found a file \(url.path).")
+            return true
+            
+        } catch {
             
             return false
         }
+    }
+    
+    func checkDirectory(_ url: URL, create: Bool) -> Bool {
         
-        return true
-        
-    } catch let error as NSError {
-        
-        if create, error.code == NSFileReadNoSuchFileError {
+        do {
+            
+            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 createDirectory(url)
+            return true
+            
+        } catch let error as NSError {
+            
+            if create, error.code == NSFileReadNoSuchFileError {
+                
+                return createDirectory(url)
+            }
+            
+            return false
         }
-        
-        return false
     }
 }