OSDN Git Service

無駄な行や改行を削除
[kcd/KCD.git] / KCD / ApplicationDirecrories.swift
1 //
2 //  ApplicationDirecrories.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/02/08.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Foundation
10
11 private func supportDirName() -> String {
12     
13     let main = Bundle.main
14     
15     return main.bundleIdentifier
16         ?? main.infoDictionary?["CFBundleName"] as? String
17         ?? main.infoDictionary?["CFBundleExecutable"] as? String
18         ?? "UnknownAppliation"
19 }
20
21 struct ApplicationDirecrories {
22     
23     static let support = searchedURL(for: .applicationSupportDirectory)
24         .appendingPathComponent(supportDirName())
25     
26     static let documents = searchedURL(for: .documentDirectory)
27     
28     static let pictures = searchedURL(for: .picturesDirectory)
29     
30     private static func searchedURL(for directory: FileManager.SearchPathDirectory) -> URL {
31         
32         return FileManager.default.urls(for: directory, in: .userDomainMask)
33             .last
34             ?? URL(fileURLWithPath: NSHomeDirectory())
35     }
36 }
37
38 func createDirectory(_ url: URL) -> Bool {
39     
40     do {
41         
42         try FileManager.default.createDirectory(at: url,
43                                                 withIntermediateDirectories: false,
44                                                 attributes: nil)
45         
46         return true
47         
48     } catch {
49         
50         return false
51         
52     }
53 }
54
55 func checkDirectory(_ url: URL) -> Bool {
56     
57     do {
58         
59         let resourceValue = try url.resourceValues(forKeys: [.isDirectoryKey])
60         if !resourceValue.isDirectory! {
61             
62             print("Expected a folder to store application data, found a file \(url.path).")
63             
64             return false
65         }
66         
67         return true
68         
69     } catch {
70         
71         let nserror = error as NSError
72         if nserror.code == NSFileReadNoSuchFileError {
73             
74             return createDirectory(url)
75         }
76         
77         return false
78     }
79 }