-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLSFileManager.swift
127 lines (107 loc) · 4.65 KB
/
LSFileManager.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// LSFileManager.swift
// LSFileManager
//
// Created by StephenChen on 16/7/7.
// Copyright © 2016年 Lansion. All rights reserved.
//
import UIKit
import Foundation
enum LSDirectory {
case HOME_DIR
case DOC_DIR
case CACHE_DIR
case LIB_DIR
case TMP_DIR
}
class LSFileManager: NSObject {
private override init() {}
static let manager = LSFileManager()
let fileManager = NSFileManager.defaultManager()
//MARK: - about directory
let homeDir = NSHomeDirectory()
let docDir = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).first!
let cacheDir = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true).first!
let libDir = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomainMask.UserDomainMask, true).first!
let tempDir = NSTemporaryDirectory()
static func printDirs(){
print("home = \(manager.homeDir)")
print("docDir = \(manager.docDir)")
print("cacheDir = \(manager.cacheDir)")
print("libDir = \(manager.libDir)")
print("tempDir = \(manager.tempDir)")
}
static func getDir(dir: LSDirectory) -> String {
switch dir {
case LSDirectory.HOME_DIR:
return manager.homeDir
case LSDirectory.DOC_DIR:
return manager.docDir
case LSDirectory.CACHE_DIR:
return manager.cacheDir
case LSDirectory.LIB_DIR:
return manager.libDir
case LSDirectory.TMP_DIR:
return manager.tempDir
}
}
//MARK: - about folder
static func listFolder(dir: LSDirectory, path: String?) -> [String]? {
return manager.fileManager.subpathsAtPath(manager.getFullPath(dir,path: path))
}
static func createFolder(folderName: String, under: LSDirectory) -> Bool {
do{
try manager.fileManager.createDirectoryAtPath(manager.getFullPath(under,path: folderName), withIntermediateDirectories: true, attributes: nil)
return true
} catch{
return false
}
}
static func deleteFile(filePath: String, under: LSDirectory) -> Bool {
do {
try manager.fileManager.removeItemAtPath(manager.getFullPath(under,path: filePath))
return true
} catch {
return false
}
}
static func isFileExisted(filePath: String, under: LSDirectory) -> Bool {
return manager.fileManager.fileExistsAtPath(manager.getFullPath(under,path: filePath))
}
//MARK: - read and write
private func getFullPath(dir: LSDirectory, path: String?) -> String{
return (path != nil) ? LSFileManager.getDir(dir) + "/" + path! : LSFileManager.getDir(dir)
}
static func writeNSData(data: NSData, toFile: String, under: LSDirectory) -> String? {
let path = manager.getFullPath(under,path: toFile)
let isWrite = data.writeToFile(path, atomically: true)
return isWrite ? path : nil
}
static func readNSData(fromFile: String, under: LSDirectory) -> NSData? {
return NSData.init(contentsOfFile: manager.getFullPath(under,path: fromFile))
}
static func writeNSArray(array: NSArray, toFile: String, under: LSDirectory) -> String? {
let path = manager.getFullPath(under,path: toFile)
let isWrite = array.writeToFile(path, atomically: true)
return isWrite ? path : nil
}
static func readNSArray(fromFile: String, under: LSDirectory) -> NSArray? {
return NSArray.init(contentsOfFile: manager.getFullPath(under, path: fromFile))
}
static func writeNSDictionary(dictionary: NSDictionary, toFile: String, under: LSDirectory) -> String? {
let path = manager.getFullPath(under, path: toFile)
let isWrite = dictionary.writeToFile(path, atomically: true)
return isWrite ? path : nil
}
static func readNSDictionary(fromFile: String, under: LSDirectory) -> NSDictionary? {
return NSDictionary.init(contentsOfFile: manager.getFullPath(under, path: fromFile))
}
static func writeUIImage(image: UIImage, toFile: String, under: LSDirectory) -> String? {
let imgData = UIImagePNGRepresentation(image)
return (imgData != nil) ? LSFileManager.writeNSData(imgData!, toFile: toFile, under: under) : nil
}
static func readUIImage(fromFile: String, under: LSDirectory) -> UIImage? {
let imgData = LSFileManager.readNSData(fromFile, under: under)
return (imgData != nil) ? UIImage.init(data: imgData!) : nil
}
}